为了账号安全,请及时绑定邮箱和手机立即绑定

如何更新matplotlib中的绘图?

如何更新matplotlib中的绘图?

梵蒂冈之花 2019-06-21 15:16:04
如何更新matplotlib中的绘图?我在这里重画这个数字有问题。我允许用户在时间尺度(x轴)中指定单元,然后重新计算并调用此函数。plots()..我希望绘图简单地更新,而不是在图形中追加另一个绘图。def plots():     global vlgaBuffSorted     cntr()     result = collections.defaultdict(list)     for d in vlgaBuffSorted:         result[d['event']].append(d)     result_list = result.values()     f = Figure()     graph1 = f.add_subplot(211)     graph2 = f.add_subplot(212,sharex=graph1)     for item in result_list:         tL = []         vgsL = []         vdsL = []         isubL = []         for dict in item:             tL.append(dict['time'])             vgsL.append(dict['vgs'])             vdsL.append(dict['vds'])             isubL.append(dict['isub'])         graph1.plot(tL,vdsL,'bo',label='a')         graph1.plot(tL,vgsL,'rp',label='b')         graph2.plot(tL,isubL,'b-',label='c')     plotCanvas = FigureCanvasTkAgg(f, pltFrame)     toolbar = NavigationToolbar2TkAgg(plotCanvas, pltFrame)     toolbar.pack(side=BOTTOM)     plotCanvas.get_tk_widget().pack(side=TOP)
查看完整描述

3 回答

?
桃花长相依

TA贡献1860条经验 获得超8个赞

你基本上有两个选择:

  1. 做你目前正在做的事情,但是打电话graph1.clear()graph2.clear()在重新绘制数据之前。这是最慢、但最简单、最健壮的选择。

  2. 您可以只更新绘图对象的数据,而不是重新绘图。您需要对代码进行一些更改,但这比每次重新绘制代码要快得多。但是,您正在绘制的数据的形状不能更改,如果数据的范围正在更改,则需要手动重置x和y轴限值。

为了给出第二种选择的例子:

import matplotlib.pyplot as pltimport numpy as np

x = np.linspace(0, 6*np.pi, 100)y = np.sin(x)# You probably won't need this if you're embedding things in a tkinter plot...plt.ion()
fig = plt.figure()ax = fig.add_subplot(111)line1, = ax.plot(x, y, 'r-')
 # Returns a tuple of line objects, thus the commafor phase in np.linspace(0, 10*np.pi, 500):
    line1.set_ydata(np.sin(x + phase))
    fig.canvas.draw()
    fig.canvas.flush_events()


查看完整回答
反对 回复 2019-06-21
?
呼唤远方

TA贡献1856条经验 获得超11个赞

您还可以这样做:这将在图上绘制一个10x1随机矩阵数据,用于for循环的50个循环。

import matplotlib.pyplot as pltimport numpy as np

plt.ion()for i in range(50):
    y = np.random.random([10,1])
    plt.plot(y)
    plt.draw()
    plt.pause(0.0001)
    plt.clf()


查看完整回答
反对 回复 2019-06-21
  • 3 回答
  • 0 关注
  • 2223 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信