如何更新matplotlib中的绘图?

如何更新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)


梵蒂冈之花
浏览 2224回答 3
3回答

桃花长相依

你基本上有两个选择:做你目前正在做的事情,但是打电话graph1.clear()和graph2.clear()在重新绘制数据之前。这是最慢、但最简单、最健壮的选择。您可以只更新绘图对象的数据,而不是重新绘图。您需要对代码进行一些更改,但这比每次重新绘制代码要快得多。但是,您正在绘制的数据的形状不能更改,如果数据的范围正在更改,则需要手动重置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()

呼唤远方

您还可以这样做:这将在图上绘制一个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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python