簡體   English   中英

Matplotlib animation 在 Colab 中無法正確顯示

[英]Matplotlib animation not displaying correctly in Colab

我知道這個問題有以前的答案,但由於某種原因,我似乎無法讓 animation 顯示。 相反,animation 的所有框架都覆蓋在空白 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

我試圖復制在這里找到的一個有點相似的項目

我相當肯定這個錯誤很小,但我無法弄清楚問題到底出在哪里。 任何幫助表示贊賞

我認為原因是在 animation function 中使用了 plt.bar。 我認為將其更改為 axes.bar() 並關閉初始圖形將完成 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

在此處輸入圖像描述

我也有同樣的問題。 感謝 Kalibril,我可以在 colab 上運行 animation。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM