Matplotlib 动画在 Colab 中无法正确显示
Matplotlib animation not displaying correctly in Colab
我知道这个问题以前有答案,但出于某种原因我似乎无法显示动画。相反,动画的所有帧都覆盖在出现在空白动画下方的图形中
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
我试图复制一个有点相似的项目 here
我相当确定这个错误很小,但我无法弄清楚问题到底出在哪里。
感谢任何帮助
我认为原因是在动画函数中使用了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
我知道这个问题以前有答案,但出于某种原因我似乎无法显示动画。相反,动画的所有帧都覆盖在出现在空白动画下方的图形中
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
我试图复制一个有点相似的项目 here
我相当确定这个错误很小,但我无法弄清楚问题到底出在哪里。 感谢任何帮助
我认为原因是在动画函数中使用了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