matplotlib动画在Colab中未正确显示

发布于 2025-01-21 15:05:46 字数 1184 浏览 4 评论 0 原文

我知道这个问题以前有答案,但由于某种原因,我似乎无法显示动画。取而代之的是,动画的所有框架都覆盖在空白动画下方的图中,

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 = plt.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

我试图复制一个有点相似的项目在这里

我可以肯定的是,错误是次要的,但我无法弄清楚问题到底在哪里。 任何帮助都将受到赞赏

I know there are previous answers to this question but for some reason I cannot seem to get the animation to show. Instead all the frames of the animation are overlayed in a figure that appears below the blank animation

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 = plt.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

I was trying to replicate a somewhat similar project found here

I'm fairly certain the mistake is minor but I cannot figure out where exactly the problem is.
Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2025-01-28 15:05:46

我认为原因是在动画函数中使用plt.bar。我认为将其更改为axes.bar()和关闭初始图将完成动画。

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

I think the cause is the use of plt.bar in the animation function. I think changing this to axes.bar() and closing the initial graph will complete the animation.

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

enter image description here

吖咩 2025-01-28 15:05:46

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

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)

I have the same problem. Thank to Kalibril, I can run animation on colab.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文