如何在matplotlib中更新绘图 [英] How to update a plot in matplotlib

查看:59
本文介绍了如何在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)

推荐答案

您基本上有两个选择:

  1. 执行您当前正在执行的操作,但是在重新绘制数据之前调用graph1.clear()graph2.clear()。这是最慢但最简单、最可靠的选项。

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

给出第二个选项的示例:

import matplotlib.pyplot as plt
import 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 comma

for phase in np.linspace(0, 10*np.pi, 500):
    line1.set_ydata(np.sin(x + phase))
    fig.canvas.draw()
    fig.canvas.flush_events()

这篇关于如何在matplotlib中更新绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆