我正在尝试做一个简单的绘图界面,该界面允许我单击以将点添加到列表中,然后用另一个键或另一个单击来调用这些点的三角剖分。

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()


谢谢

最佳答案

我认为您已经拥有所需的一切,以获得想要的点击点的三倍。您只需要将第二个代码移到第一个的__call__中,并对其进行调整以使用以前选择的点即可。

import numpy as np
from scipy.spatial import Delaunay
from matplotlib import pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs = []
        self.ys = []
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        if event.inaxes!=self.line.axes: return
        if event.button == 1:
            self.xs.append(event.xdata)
            self.ys.append(event.ydata)
            self.line.set_data(self.xs, self.ys)
        elif event.button == 3:
            points=np.c_[self.xs, self.ys]
            tri = Delaunay(points)
            self.line.axes.triplot(points[:,0], points[:,1], tri.simplices.copy())
        self.line.figure.canvas.draw()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('left click to choose points, \n \
             right click to plot delaunay triangulation')
line, = ax.plot([], [], marker="o", ms=10, ls="")  # empty line
linebuilder = LineBuilder(line)

plt.show()


python - 使用Python和Matplotlib进行交互式三角剖分-LMLPHP

为了显示在绘制的每个新点上的倾斜以及能够重新启动整个交互,解决方案将稍微复杂一些。然后,需要检查轴上是否已经有绘图,并在绘制新绘图之前将其删除。

import numpy as np
from scipy.spatial import Delaunay
from matplotlib import pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
        self.trip = None
        self.reset()

    def reset(self):
        self.xs = []
        self.ys = []
        if self.trip:
            for t in self.trip:
                t.remove()
        self.trip = None
        self.line.set_data(self.xs, self.ys)

    def __call__(self, event):
        if event.inaxes!=self.line.axes: return
        if event.button == 1:
            self.xs.append(event.xdata)
            self.ys.append(event.ydata)
            self.line.set_data(self.xs, self.ys)
            points=np.c_[self.xs, self.ys]
            if len(self.xs) >= 3:
                tri = Delaunay(points)
                if self.trip:
                    for t in self.trip:
                        t.remove()
                self.trip = self.line.axes.triplot(points[:,0], points[:,1],
                                                 tri.simplices.copy(), color="C0")
        elif event.button==3:
            self.reset()

        self.line.figure.canvas.draw()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis([0,1,0,1])
ax.set_title('left click to choose points,right click restart')
line, = ax.plot([], [], marker="o", ms=10, ls="")  # empty line
linebuilder = LineBuilder(line)

plt.show()

关于python - 使用Python和Matplotlib进行交互式三角剖分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48319753/

10-12 14:25