-
效果图
-
效果图1
-
效果图2
-
-
导入支持包
import numpy as np import matplotlib.pyplot as plt
-
生成测试数据
x = np.linspace(0, 6, 40)
-
打开交互模式
plt.ion() # 开启交互模型
-
动态绘图
所谓的动态,就是在绘制的多张图片之间切换,这里通过for循环来每轮绘制一个图像
for i in range(100): plt.cla() # 每轮绘图清空画布,如果注释掉这行,结果将会如效果图2所示 plt.ylim(-1.5,1.5) # y轴值的范围,如果不写定可能会造成y轴的抖动 plt.plot(x,np.sin(x), c='red') plt.plot(x,np.cos(x), c='green') plt.pause(0.01) # 暂停0.01s x=x+0.1 # 改变数据,以供下一轮画图
其中
plt.cla()
的作用是在每轮绘图前清空画布,如果注释了该行,显示结果将会如效果图2所示 -
关闭交互模式
plt.ioff() # 关闭交互模式
-
出图
plt.pause(0) # 出图,这种方式绘图最终画面会停止在屏幕上 # plt.show() # 出图,这种方式绘图结束后会关闭画面
出图效果如文首效果图1所示
Matplotlib绘制动态图像
猜你喜欢
转载自blog.csdn.net/BBJG_001/article/details/104533593
今日推荐
周排行