animation.Funcanimation() Matplotlib / 如何正确使用它?
animation.Funcanimation() Matplotlib / How to use it properly?
示例数据:
Area Africa Asia Europe N_America S_America Australia Antarctica
Y1961 -0.17 0.49 8.74 0.17 1.16 1.51 2.80
Y1962 -0.40 -0.08 3.00 -0.58 -1.39 1.23 -2.27
Y1963 0.64 2.66 1.03 3.72 1.39 -1.14 1.35
Y1964 -1.53 -1.66 -5.23 -2.85 -2.17 -0.22 -3.66
Y1965 -1.97 -0.33 -4.30 -5.94 -0.15 1.27 -0.99
代码:
n=len(Continents.index)
fig,ax=plt.subplots()
def action(curr):
global colors
global cmap
global Continents
global color_dict
if curr==n:
a.event_source.stop()
CURR=Continents.iloc[curr]
ax = plt.gca()
ticks(curr)
bars = plt.bar(Continents.columns,CURR,color=[color_dict[country] for country in Continents.columns])
for bar,height in zip(bars,CURR):
return bar.set_height(height)
a = animation.FuncAnimation(fig,action, interval=100, frames=np.array(range(0,n)))
当我单独使用我的动作函数时,它可以工作,但我不能在 animation.Funcanimation() 函数中使用它。我想根据年份更改条形高度。
试试这个。颜色由 'colors'.
指定
import pandas as pd
import numpy as np
import io
data = '''
Area Africa Asia Europe N_America S_America Australia Antarctica
Y1961 -0.17 0.49 8.74 0.17 1.16 1.51 2.80
Y1962 -0.40 -0.08 3.00 -0.58 -1.39 1.23 -2.27
Y1963 0.64 2.66 1.03 3.72 1.39 -1.14 1.35
Y1964 -1.53 -1.66 -5.23 -2.85 -2.17 -0.22 -3.66
Y1965 -1.97 -0.33 -4.30 -5.94 -0.15 1.27 -0.99
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# from matplotlib.animation import PillowWriter
idx = df.columns[1:].tolist()
colors = ['m','g','c','k','y']
fig = plt.figure()
ax = plt.axes(xlim=(0,6), ylim=(-10, 10))
# ax.set_xticklabels(idx, rotation=30)
def animate(i):
ax.cla()
ax.bar(idx, df.iloc[i,1:], color=colors)
ax.set_ylim(-10, 10)
return fig,
anim = FuncAnimation(fig, animate, frames=5, interval=200, repeat=False)
plt.show()
# anim.save('bar_ani.gif', writer='pillow')
示例数据:
Area Africa Asia Europe N_America S_America Australia Antarctica
Y1961 -0.17 0.49 8.74 0.17 1.16 1.51 2.80
Y1962 -0.40 -0.08 3.00 -0.58 -1.39 1.23 -2.27
Y1963 0.64 2.66 1.03 3.72 1.39 -1.14 1.35
Y1964 -1.53 -1.66 -5.23 -2.85 -2.17 -0.22 -3.66
Y1965 -1.97 -0.33 -4.30 -5.94 -0.15 1.27 -0.99
代码:
n=len(Continents.index)
fig,ax=plt.subplots()
def action(curr):
global colors
global cmap
global Continents
global color_dict
if curr==n:
a.event_source.stop()
CURR=Continents.iloc[curr]
ax = plt.gca()
ticks(curr)
bars = plt.bar(Continents.columns,CURR,color=[color_dict[country] for country in Continents.columns])
for bar,height in zip(bars,CURR):
return bar.set_height(height)
a = animation.FuncAnimation(fig,action, interval=100, frames=np.array(range(0,n)))
当我单独使用我的动作函数时,它可以工作,但我不能在 animation.Funcanimation() 函数中使用它。我想根据年份更改条形高度。
试试这个。颜色由 'colors'.
指定import pandas as pd
import numpy as np
import io
data = '''
Area Africa Asia Europe N_America S_America Australia Antarctica
Y1961 -0.17 0.49 8.74 0.17 1.16 1.51 2.80
Y1962 -0.40 -0.08 3.00 -0.58 -1.39 1.23 -2.27
Y1963 0.64 2.66 1.03 3.72 1.39 -1.14 1.35
Y1964 -1.53 -1.66 -5.23 -2.85 -2.17 -0.22 -3.66
Y1965 -1.97 -0.33 -4.30 -5.94 -0.15 1.27 -0.99
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# from matplotlib.animation import PillowWriter
idx = df.columns[1:].tolist()
colors = ['m','g','c','k','y']
fig = plt.figure()
ax = plt.axes(xlim=(0,6), ylim=(-10, 10))
# ax.set_xticklabels(idx, rotation=30)
def animate(i):
ax.cla()
ax.bar(idx, df.iloc[i,1:], color=colors)
ax.set_ylim(-10, 10)
return fig,
anim = FuncAnimation(fig, animate, frames=5, interval=200, repeat=False)
plt.show()
# anim.save('bar_ani.gif', writer='pillow')