python - Matplotlib 在子图中为多个条形图设置动画
问题描述
我已经使用 FuncAnimation 成功地为单个条形图制作了动画,但我现在希望在子图中同时有 3 个条形图。我不知道我的动画函数应该返回什么。我试图为一个条形图调整我的代码以使其适用于 3。这是我的尝试,但我收到一条错误消息,提示“AttributeError:'BarContainer' 对象没有属性'get_zorder'”。我的数据“soln”是一个 3x1000x10 的列表。3 用于每个子图,10 用于每个条。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
soln = np.array([[[1, 0, 0],[1,1,0],[1, 2, 3]],[[1, 0, 0],[1,1,0],[1, 2, 3]],[[1, 0, 0],[1,1,0],[1, 2, 3]]])
print(soln.shape)
def animate(frame):
for i, list in enumerate(rects):
for j, rect in enumerate(list):
rect.set_height(soln[i, frame, j])
return rects
fig, axs = plt.subplots(1, 3)
xs = range(0, 3)
rects = []
for i in range(soln.shape[0]):
rects.append(axs[i].bar(xs, soln[i, 0, :]))
ani = animation.FuncAnimation(fig, animate, blit=True, repeat=False)
解决方案
推荐阅读
- javascript - 如何缩放条形图列以正确且按比例地放入其容器中?
- swift - iOS 应用上的 Readium 和 Sony URMS 集成
- java - 没有从 jsp 页面得到正确的响应
- javascript - 从另一个 javascript 推送角度图表数据
- python - 如何防止转义字符改变随机生成的 Python 字符串的输出?
- scala - 尝试与尝试 - 捕获块
- azure-service-fabric - 在开发人员机器上自动创建没有 DefaultServices 的服务
- html - 使用 itext 和飞碟从 html/css 转换为 pdf 时字体颜色发生变化
- android - android-删除一个项目 SwipeListView 错误
- python - Django在分配给数据库之前获取POST id