用python数据可视化做一个柱形动态图
时间: 2023-10-08 12:03:50 浏览: 50
好的,以下是一个用 Python 数据可视化库 Matplotlib 和动画库 FuncAnimation 制作柱形动态图的示例代码:
```python
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
# 创建画布和子图
fig, ax = plt.subplots()
# 生成数据
x = np.arange(10)
y = np.random.randint(1, 10, 10)
# 绘制初始柱形图
rects = ax.bar(x, y)
# 定义更新函数
def update(num):
# 更新数据
y = np.random.randint(1, 10, 10)
for rect, h in zip(rects, y):
rect.set_height(h)
# 设置坐标轴范围
ax.set_xlim(-1, 10)
ax.set_ylim(0, 10)
# 返回更新后的图形元素
return rects,
# 创建动画
ani = FuncAnimation(fig, update, frames=range(10), interval=500)
# 显示动画
plt.show()
```
运行以上代码,可以看到一个动态的柱形图。每隔 500ms,随机更新一次柱形图的高度。你可以根据自己的需求修改数据、坐标轴范围、动画帧率等参数,制作出自己想要的动态柱形图。