Matplotlib 动画,一段时间后条形变白
Matplotlib animation, bars are getting white after a while
我做错了什么?谁能帮我?或者给我特定的关键字进行 google 搜索(我确定我不是第一个)?现在已经处理这个问题超过 8 小时了,在互联网上找不到任何东西。
完整笔记本Link(最后的问题):Kaggle Notebook
我的代码:
dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()
bar_label = []
for key in dict_data:
bar_label.append(key)
bar_color = generate_color_series(28)
fig = plt.figure(figsize=(7, 5))
axes = fig.add_subplot(1, 1, 1)
axes.set_xlim(0, 35)
axes.set_xlabel("Popularity in %")
def animate(i):
i_value = []
for key in dict_data:
i_value.append(dict_data[key][i])
i_value = tuple(i_value)
plt.barh(bar_label, i_value, color=bar_color)
ani = FuncAnimation(fig, animate, interval=30)
%time ani.save('myAnimation1.gif', writer='imagemagick', fps=15)
plt.close()
输出:
[图片]
原因是在绘制新图形时,之前的图形仍然完好无损,如评论中所述。因此,最简单的处理方法是将清除当前图形的动作放在循环过程中。清除图形会删除 x 轴限制并更改条形图的高度,因此会再次添加 x 轴限制。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# set global variable for color palette (plots) and grid style
PALETTE = "magma_r" # my favourite palettes: flare, CMRmap_r, magma_r
sns.set(style="darkgrid")
# function that generates n color values out of defined PALETTE
def generate_color_series(n):
segments = cm.get_cmap(PALETTE, n)
return segments(range(n))
data = pd.read_csv('./data/Most Popular Programming Languages from 2004 to 2022.csv', sep=',')
data["Date"] = pd.to_datetime(data["Date"])
dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()
bar_label = []
for key in dict_data:
bar_label.append(key)
bar_color = generate_color_series(28)
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(0, 35)
ax.set_xlabel("Popularity in %")
def animate(i):
i_value = []
for key in dict_data:
i_value.append(dict_data[key][i])
i_value = tuple(i_value)
ax.cla()
ax.set_xlim(0, 35)
ax.barh(bar_label, i_value, color=bar_color)
ani = FuncAnimation(fig, animate, interval=30)
from IPython.display import HTML
plt.close()
HTML(ani.to_html5_video())
我做错了什么?谁能帮我?或者给我特定的关键字进行 google 搜索(我确定我不是第一个)?现在已经处理这个问题超过 8 小时了,在互联网上找不到任何东西。
完整笔记本Link(最后的问题):Kaggle Notebook
我的代码:
dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()
bar_label = []
for key in dict_data:
bar_label.append(key)
bar_color = generate_color_series(28)
fig = plt.figure(figsize=(7, 5))
axes = fig.add_subplot(1, 1, 1)
axes.set_xlim(0, 35)
axes.set_xlabel("Popularity in %")
def animate(i):
i_value = []
for key in dict_data:
i_value.append(dict_data[key][i])
i_value = tuple(i_value)
plt.barh(bar_label, i_value, color=bar_color)
ani = FuncAnimation(fig, animate, interval=30)
%time ani.save('myAnimation1.gif', writer='imagemagick', fps=15)
plt.close()
输出: [图片]
原因是在绘制新图形时,之前的图形仍然完好无损,如评论中所述。因此,最简单的处理方法是将清除当前图形的动作放在循环过程中。清除图形会删除 x 轴限制并更改条形图的高度,因此会再次添加 x 轴限制。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# set global variable for color palette (plots) and grid style
PALETTE = "magma_r" # my favourite palettes: flare, CMRmap_r, magma_r
sns.set(style="darkgrid")
# function that generates n color values out of defined PALETTE
def generate_color_series(n):
segments = cm.get_cmap(PALETTE, n)
return segments(range(n))
data = pd.read_csv('./data/Most Popular Programming Languages from 2004 to 2022.csv', sep=',')
data["Date"] = pd.to_datetime(data["Date"])
dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()
bar_label = []
for key in dict_data:
bar_label.append(key)
bar_color = generate_color_series(28)
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(0, 35)
ax.set_xlabel("Popularity in %")
def animate(i):
i_value = []
for key in dict_data:
i_value.append(dict_data[key][i])
i_value = tuple(i_value)
ax.cla()
ax.set_xlim(0, 35)
ax.barh(bar_label, i_value, color=bar_color)
ani = FuncAnimation(fig, animate, interval=30)
from IPython.display import HTML
plt.close()
HTML(ani.to_html5_video())