Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除点/线画布Python Matplotlib_Python_Matplotlib_Canvas_Pyqt5 - Fatal编程技术网

删除点/线画布Python Matplotlib

删除点/线画布Python Matplotlib,python,matplotlib,canvas,pyqt5,Python,Matplotlib,Canvas,Pyqt5,我使用以下代码在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.

我使用以下代码在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):
    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_idle()
    print(self.xs)
    print(self.ys)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0], marker="o", linestyle="") 
linebuilder = LineBuilder(line)
plt.show()
是否可以删除相同的行?例如,如果我的点2不在正确的位置,则我希望删除完整的线和点


我怎样才能做到这一点呢?

既然你正在构建一个比简单地创建一个用户点击点更复杂的交互,我建议你使用一个按钮

您需要准确地定义要执行的操作:删除最后一个点,删除所有点,删除所有点,但用于初始化的点除外

我将向您展示如何创建一个
Reset
按钮,根据Matplotlib的文档删除所有点

首先,创建一个按钮将填充的
Axes
对象。 您需要调整主斧头,以使两者不会重叠

from matplotlib.widgets import Button

plt.subplots_adjust(bottom=0.2)
breset_ax = plt.axes([0.7, 0.05, 0.1, 0.075])
breset = Button(breset_ax, 'Reset')
然后设置按钮的回调。 我发现在
LineBuilder
类中定义该回调相关,因为它将清除封装的点

class LineBuilder:
    ...

    def reset(self, _event):
        self.xs = []
        self.ys = []
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw_idle()
然后,将该回调绑定到按钮:

breset.on_clicked(linebuilder.reset)
这会给你一些类似的东西:

单击重置按钮将删除所有已绘制的点


完整代码:

from matplotlib import pyplot as plt
from matplotlib.widgets import Button

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):
        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_idle()
        print(self.xs)
        print(self.ys)

    def reset(self, _event):
        self.xs = []
        self.ys = []
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw_idle()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0], marker="o", linestyle="")
linebuilder = LineBuilder(line)

plt.subplots_adjust(bottom=0.2)
breset_ax = plt.axes([0.7, 0.05, 0.1, 0.075])
breset = Button(breset_ax, 'Reset')
breset.on_clicked(linebuilder.reset)

plt.show()

您是否可以编写一个函数来使用按键按事件删除拾取的点? 因此,用户将使用鼠标拾取一个点,并使用“删除按钮”删除该点

像这样的事情:

def on_key(event):
    if event.key == u'delete':
        ax = plt.gca()
        if ax.picked_object:
            ax.picked_object.remove()
            ax.picked_object = None
            ax.figure.canvas.draw()