【问题标题】:Collect points from a figure and plot them从图中收集点并绘制它们
【发布时间】:2021-09-13 12:16:46
【问题描述】:

我正在使用 plt.connect 将光标经过的点保存到坐标列表中,并且我想绘制这些点,但是在断开绘图后我无法做任何事情并且绘图自行关闭. 我找不到任何例子,在连接情节之后,你可以对情节做任何事情。 这是我要修复的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backend_bases import MouseButton
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t) 
fig, ax = plt.subplots()
ax.plot(t, s)  #just plot something
x=[]
y=[]
def on_move(event):
    global x,y
    if event.inaxes:
        x.append(event.xdata)
        y.append(event.ydata)
        print('data coords %f %f' % (event.xdata, event.ydata))
plt.waitforbuttonpress()
binding_id = plt.connect('motion_notify_event', on_move)
plt.pause(5)  #5 seconds to collect data
plt.disconnect(binding_id) #here it closes by itself
ax.plot(x,y)

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    只在最后一行再次暂停。这将保留图表,您将可以看到线条。

    ...    
    ax.plot(x,y)
    plt.pause(5)
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多