如何将用户在 Matplotlib 中输入的点传递给 np.array?
How do I pass on points that the user entered in Matplotlib to a np.array?
我想创建一个程序,允许用户首先在 Matplotlib 绘图中输入点,然后使用这些点创建 Voronoi 图。
弄清楚了 2 个部分,但没有联系。如何传递用户输入的点并在 Voronoi 部分使用它? (我只需要知道如何将 points = np.random.rand(20,2)
更改为用户输入的点数。)
绘制点:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
def onclick(event):
print('x=%d, y=%d, xdata=%f, ydata=%f' %
(event.x, event.y, 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()`
创建 Voronoi 图:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d
fig = plt.figure()
points = np.random.rand(20,2)
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()
您可以只将 x-y 对保存在列表中,稍后再将其转换为数组:
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()
只要用户在绘图中点击,点就会添加到列表中,直到用户关闭原始图形。之后创建一个新图形并绘制 Voronoi 图。希望这有帮助。
您可以使用 class 来存储点,然后在同一图中绘制图表。在以下示例中,您将使用鼠标左键放置点并单击鼠标右键绘制 voronoi 图。如果你以后想在图中添加新的点,你可以这样做。
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])
class V():
def __init__(self, ax = None):
self.ax = ax
if not 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 = []
def onclick(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()
def voronoi(self):
self.ax.clear()
vor = Voronoi(self.points)
voronoi_plot_2d(vor, ax=self.ax)
v = V(ax=ax)
plt.show()
我想创建一个程序,允许用户首先在 Matplotlib 绘图中输入点,然后使用这些点创建 Voronoi 图。
弄清楚了 2 个部分,但没有联系。如何传递用户输入的点并在 Voronoi 部分使用它? (我只需要知道如何将 points = np.random.rand(20,2)
更改为用户输入的点数。)
绘制点:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
def onclick(event):
print('x=%d, y=%d, xdata=%f, ydata=%f' %
(event.x, event.y, 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()`
创建 Voronoi 图:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d
fig = plt.figure()
points = np.random.rand(20,2)
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()
您可以只将 x-y 对保存在列表中,稍后再将其转换为数组:
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()
只要用户在绘图中点击,点就会添加到列表中,直到用户关闭原始图形。之后创建一个新图形并绘制 Voronoi 图。希望这有帮助。
您可以使用 class 来存储点,然后在同一图中绘制图表。在以下示例中,您将使用鼠标左键放置点并单击鼠标右键绘制 voronoi 图。如果你以后想在图中添加新的点,你可以这样做。
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])
class V():
def __init__(self, ax = None):
self.ax = ax
if not 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 = []
def onclick(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()
def voronoi(self):
self.ax.clear()
vor = Voronoi(self.points)
voronoi_plot_2d(vor, ax=self.ax)
v = V(ax=ax)
plt.show()