首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Matplotlib动画在Colab中显示不正确

Matplotlib动画在Colab中显示不正确
EN

Stack Overflow用户
提问于 2022-04-15 02:13:41
回答 2查看 380关注 0票数 2

我知道以前有这个问题的答案,但出于某种原因,我似乎无法让动画展示。相反,动画的所有帧都被覆盖在一个出现在空白动画下面的图形中。

代码语言:javascript
运行
复制
from matplotlib import animation
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from matplotlib import rc
rc('animation', html='jshtml')

# This is setup code
class_capacity = [100, 100, 100]
classes = ["CS1301", "CS1331", "CS1332"]
current_enrolled_students = [10, 0, 0]
fig, axes = plt.subplots(figsize=(8,6))
#axes =fig.add_subplot()
axes.set_ylim(0, 100)

cmap = plt.get_cmap("jet")
代码语言:javascript
运行
复制
def animate(i):
    axes.clear()
    axes.set_ylim(0, 100)
    for i in range(len(current_enrolled_students)):
        current_enrolled_students[i] = random.randint(0, class_capacity[i])
    barlist = plt.bar(classes, current_enrolled_students)
    for i in range(len(barlist)):
        barlist[i].set_color(cmap(current_enrolled_students[i] / class_capacity[i]))
代码语言:javascript
运行
复制
ani = FuncAnimation(fig, animate, interval=400, blit=False, frames=9, repeat=False)
#plt.close()
#plt.show()
ani

我试图复制一个与这里类似的项目

我很确定这个错误是小的,但我不知道问题到底出在哪里。任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-15 04:05:56

我认为原因是在动画功能中使用了plt.bar。我认为将其更改为axes.bar()并关闭初始图形将完成动画。

代码语言:javascript
运行
复制
from matplotlib import animation
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from matplotlib import rc
rc('animation', html='jshtml')

# This is setup code
class_capacity = [100, 100, 100]
classes = ["CS1301", "CS1331", "CS1332"]
current_enrolled_students = [10, 0, 0]
fig, axes = plt.subplots(figsize=(8,6))
#axes =fig.add_subplot()
axes.set_ylim(0, 100)

cmap = plt.get_cmap("jet")

def animate(i):
    axes.clear()
    #axes.set_ylim(0, 100)
    for i in range(len(current_enrolled_students)):
        current_enrolled_students[i] = random.randint(0, class_capacity[i])
    barlist = axes.bar(classes, current_enrolled_students)
    for i in range(len(barlist)):
        barlist[i].set_color(cmap(current_enrolled_students[i] / class_capacity[i]))

ani = FuncAnimation(fig, animate, interval=400, blit=False, frames=9, repeat=False)
plt.close()
#plt.show()
ani

票数 1
EN

Stack Overflow用户

发布于 2022-09-25 14:58:14

我也有同样的问题。感谢Kalibril,我可以在colab上运行动画。

代码语言:javascript
运行
复制
import matplotlib.animation
import matplotlib.pyplot as plt
from itertools import count
import random

plt.rcParams["animation.html"] = "jshtml"
plt.rcParams['figure.dpi'] = 150  

plt.ioff()
fig, ax = plt.subplots()
x_value = []
y_value = []
index = count();
def animate(t):
    x_value.append(next(index))
    y_value.append(random.randint(0,10))
    ax.cla()
    ax.plot(x_value,y_value)
    ax.set_xlim(0,10)

matplotlib.animation.FuncAnimation(fig, animate, frames=10, interval = 500)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71879373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档