如何在matplotlib中更新绘图

编程入门 行业动态 更新时间:2024-10-11 17:26:00
本文介绍了如何在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) 推荐答案

您基本上有两个选择:

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

  • 可以只更新打印对象的数据,而不需要重新打印。您需要对代码进行一些更改,但这应该比每次重新打印要快得多。但是,您正在绘制的数据的形状不能更改,如果您的数据范围正在更改,您需要手动重置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中更新绘图

    本文发布于:2023-07-18 15:44:52,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1146355.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何在   matplotlib

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!