鼠标可以用作matplotlib的画笔吗?

时间:2017-03-03 12:10:12

标签: python matplotlib

我想要一个onclick事件来覆盖用户在具有特定颜色的图形中点击的坐标。

注意:

我不想实际编辑图片。这仅在显示的图上,作为用户点击位置的指示性度量。

1 个答案:

答案 0 :(得分:3)

您可以在matplotlib上发布LineBuilder示例event handling tutorial page

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 add points')
line, = ax.plot([], [], linestyle="none", marker="o", color="r")
linebuilder = LineBuilder(line)

plt.show()
相关问题