Skip to content Skip to sidebar Skip to footer

How Do I Pass On Points That The User Entered In Matplotlib To A Np.array?

I want to create a program that allows the user to first enter points in a Matplotlib plot and then creates a Voronoi Diagram using those points. Figured out the 2 parts, but not t

Solution 1:

You may use a class to store the points and later plot the diagram in that same plot. In the following example, you would place your points with the left mouse button and plot the voronoi diagram clicking the right mouse button. If you later want to add new points to the plot, you can do that.

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d

fig, ax  = plt.subplots()
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

classV():
    def__init__(self, ax = None):
        self.ax = ax
        ifnot self.ax: self.ax = plt.gca()
        tx = "left click to place points\nright click to plot voronoi diagram"
        self.text = self.ax.text(0.1,0.9, tx, transform=self.ax.transAxes, 
                                 ha="left", va="top")
        self.cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
        self.points = []

    defonclick(self, event):
        self.text.set_visible(False)
        if event.button == 1:
            print('x=%d, y=%d, xdata=%f, ydata=%f' %
                  (event.x, event.y, event.xdata, event.ydata))
            plt.plot(event.xdata, event.ydata, 'bo')
            self.points.append((event.xdata, event.ydata))
        else:
            self.voronoi()
        fig.canvas.draw()

    defvoronoi(self):
        self.ax.clear()
        vor = Voronoi(self.points)
        voronoi_plot_2d(vor, ax=self.ax)


v = V(ax=ax)
plt.show()

enter image description here

Solution 2:

You can just save your x-y pairs in a list and convert it into an array later:

import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d


fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])


points = []
def onclick(event):
    print('x=%d, y=%d, xdata=%f, ydata=%f' %
          (event.x, event.y, event.xdata, event.ydata))
    points.append([event.xdata,event.ydata])
    plt.plot(event.xdata, event.ydata, 'bo')
    fig.canvas.draw()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

fig2 = plt.figure()

vor = Voronoi(np.array(points))

voronoi_plot_2d(vor)

plt.show()

As long as the user clicks in the plot, points are added to the list, until the user closes the original figure. After that a new figure is created and the Voronoi plot is drawn. Hope this helps.

Post a Comment for "How Do I Pass On Points That The User Entered In Matplotlib To A Np.array?"