【发布时间】:2018-06-27 10:25:48
【问题描述】:
我正在尝试做一个简单的绘图界面,允许我单击以将点添加到列表中,然后用另一个键或另一个单击,调用这些点的三角剖分。
Matplotlib 提供了一个向直线添加点的小示例,但我不知道如何制作,所以我只需将点添加到列表中,然后调用函数进行三角剖分
from matplotlib import pyplot as plt
class LineBuilder:
def __init__(self, line):
self.line = line
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
print('click', event)
if event.inaxes!=self.line.axes: return
self.xs.append(event.xdata)
self.ys.append(event.ydata)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0]) # empty line
linebuilder = LineBuilder(line)
plt.show()
我使用 scikit delunay 三角测量
import numpy as np
from scipy.spatial import Delaunay
points=np.array([[134,30],[215,114],[160,212],[56,181],[41,78]])
tri = Delaunay(points)
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()
谢谢
【问题讨论】:
-
这些似乎是非常不同的问题。我建议查看the user guide 以了解与正在使用的对象相关的任何内容,并在实际问题上保持这个问题的清洁。
-
谢谢,我删除了第二个问题,因为它和你说的很不一样。
标签: python matplotlib interactive