import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
y=np.random.random(150)*10
fig, ax = plt.subplots(figsize=(12,4)) #figsize属性设置窗口大小
ax.set(xlim=(-1, 50), #X轴从-1开始,到10结束,没有这个参数x轴的原点不是0
ylim=(0,10),
title="stem chart") #y轴从0开始,到10结束,没有这个参数x轴的原点不是0
# 清空当前帧
def init():
return ax.stem(y)
# 更新新一帧的数据
def update(frame):
y[:-1] = y[1:]
y[-1] = np.random.random(1) * 10
return ax.stem(y)
# 调用 FuncAnimation
ani = FuncAnimation(fig
, update # 回调函数,每次更新时调用
, init_func=init #自定义开始帧,即传入刚定义的函数init,初始化函数。
, frames=200 #动画 frame 的取值范围,在函数运行时,其值会传递给函数 update(n) 的形参 n。若每一帧的图画与帧数相关可以使用update的参数frame
, interval=10 # frame之间的更新频率,以 ms 计
, blit=True #选择更新所有点,还是仅更新产生变化的点。
)
plt.show()
Matplotlib 干图动图
于 2023-05-11 11:23:23 首次发布