在 Python 中使用 matplotlib.animation 的动画 3D 条形图示例

Rog*_*che 2 python plot matplotlib bar-chart

我见过一些很好的模块使用示例matplotlib.animation,包括这个动画 3D 绘图示例。我想知道这个动画模块是否可以与图表一起使用bar3d

有人可以生成一个简单的例子吗?

注意:我目前正在研究一个不同的解决方案,其中不包括matplotlib.animation(请参阅我的其他帖子),但这似乎太慢了......

adr*_*nus 5

update_bars()这是一个带有 2x2 条形的小示例,在调用时,该条形将随机增长并改变颜色,一次一个:

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import random

def update_bars(num, bars):
    i = random.randint(0, 3)
    dz[i] += 0.1
    bars[i] = ax.bar3d(xpos[i], ypos[i], zpos[i], dx[i], dy[i], dz[i], color=random.choice(['r', 'g', 'b']))
    return bars

fig = plt.figure()
ax = p3.Axes3D(fig)
xpos = [1, 1, 3, 3]
ypos = [1, 3, 1, 3]
zpos = [0, 0, 0, 0]
dx = [1, 1, 1, 1]
dy = [1, 1, 1, 1]
dz = [3, 2, 6, 5]

# add bars
bars = []
for i in range(4):
    bars.append(ax.bar3d(xpos[i], ypos[i], zpos[i], dx[i], dy[i], dz[i], color=random.choice(['r', 'g', 'b'])))
ax.set_title('3D bars')

line_ani = animation.FuncAnimation(fig, update_bars, 20, fargs=[bars], interval=100, blit=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)

输出(这里没有动画):

阴谋