您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-如何在matplotlib中更新图?

Python-如何在matplotlib中更新图?

你基本上有两个选择:

精确执行当前操作,但在重新配置数据之前先致电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()
python 2022/1/1 18:25:02 有176人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶