Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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
事件处理matplotlip python_Python_Events_Matplotlib - Fatal编程技术网

事件处理matplotlip python

事件处理matplotlip python,python,events,matplotlib,Python,Events,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

我想从图形中拾取和绘制点,并将它们的坐标存储在数组中。可能吗?如果是的话,你能帮我一个忙吗?请引导我,谢谢你的帮助

我有这个例子,但它是在画一条线:

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

首先,为了绘制点而不是线,您需要定义一个标记,并可以选择关闭该线:

line, = ax.plot([0], [0], marker="o", linestyle="")
坐标已存储在数组
LineBuilder.xs
LineBuilder.xs
中,因此您可以直接打印它们

完整示例:

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

首先,为了绘制点而不是线,您需要定义一个标记,并可以选择关闭该线:

line, = ax.plot([0], [0], marker="o", linestyle="")
坐标已存储在数组
LineBuilder.xs
LineBuilder.xs
中,因此您可以直接打印它们

完整示例:

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()
很好,在这种情况下,你可能会找到答案。你可能还想看看一般情况下SO是如何工作的。很好,在这种情况下,你可能会找到答案。您可能还想采取行动,看看一般情况下,SO是如何工作的。