使用 Python 和 Matplotlib 进行交互式点三角测量

Zlo*_*niy 2 python interactive matplotlib

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

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()
Run Code Online (Sandbox Code Playgroud)

我使用 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()
Run Code Online (Sandbox Code Playgroud)

谢谢

Imp*_*est 5

我认为您已经拥有了获得所需点击点三重图所需的一切。您只需要将第二个代码移到__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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

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()
Run Code Online (Sandbox Code Playgroud)