【问题标题】:Updating Matplotlib plot when clicked单击时更新 Matplotlib 图
【发布时间】:2017-03-18 23:26:43
【问题描述】:

当用户单击图表时,我正在尝试使用新数据点更新 matplotlib 图。我怎样才能做到这一点?我试过使用 plt.draw() 之类的东西但没有成功。到目前为止,这是我的尝试。我觉得我在这里遗漏了一些基本的东西。

x=[1]
y=[1]

def onclick(event):
    if event.button == 1:
         x.append(event.xdata)
         y.append(event.ydata)

fig,ax=plt.subplots()
ax.scatter(x,y)
fig.canvas.mpl_connect('button_press_event',onclick)
plt.show()
plt.draw()

【问题讨论】:

    标签: python events matplotlib


    【解决方案1】:

    您绑定事件的方式没有任何问题。但是,您只更新了数据集,并没有告诉matplotlib 使用新数据重新绘制。为此,我在onclick 方法中添加了最后4 行。它们是不言自明的,但也有 cmets。

    import matplotlib.pyplot as plt
    x = [1];
    y = [1];
    
    def onclick(event):
        if event.button == 1:
             x.append(event.xdata)
             y.append(event.ydata)
        #clear frame
        plt.clf()
        plt.scatter(x,y); #inform matplotlib of the new data
        plt.draw() #redraw
    
    fig,ax=plt.subplots()
    ax.scatter(x,y)
    fig.canvas.mpl_connect('button_press_event',onclick)
    plt.show()
    plt.draw()
    

    注意:我的电脑 (ubuntu 14.04) 中的 matplotlib 会更改比例,因此您可能想研究如何修复 x-y 比例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多