matplotlib动态更新图
时间: 2023-09-03 12:09:11 浏览: 158
你可以使用以下代码来实现动态更新图表:
```python
import time
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
if __name__ == '__main__':
# 启用交互模式
plt.ion()
# 创建一个图形和一组子图
figure, ax = plt.subplots()
# 使用lines保存绘图对象
lines, = ax.plot([], [])
ax.set_autoscaley_on(True)
ax.grid()
for n in range(600):
# 生成数据
xdata = np.arange(128)
ydata = rand(128)
# 更新数据
lines.set_xdata(xdata)
lines.set_ydata(ydata)
# 重新缩放
ax.relim()
ax.autoscale_view()
# 绘制并刷新图形
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.01)
```
这段代码使用matplotlib库实现了动态绘图的功能。首先启用交互模式,并创建一个图形对象和子图对象。然后,通过循环生成随机数据,并更新图表数据。最后,绘制并刷新图形,以实现动态更新图表的效果。你可以根据需要调整数据生成和更新的逻辑,以满足你的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [matplotlib 动态刷新绘图(最简单的方法)](https://blog.csdn.net/falwat/article/details/123306390)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [jupyter notebook 实现matplotlib图动态刷新](https://download.csdn.net/download/weixin_38644141/14910441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文