【发布时间】:2015-03-13 07:33:57
【问题描述】:
我是使用 Matplotlib 制作动画的新手,遇到了一些麻烦。我想创建一个粒子位置的动画,我想在每一步显示帧号。我在代码 sn-p 的开头创建了示例数据,因此代码是独立的(通常我的数据是从 csv 读取的)。
问题 - 显示的绘图完全空白。但是,如果我注释掉 time_text 的返回(即将“返回补丁,time_text”更改为“返回补丁”)一切正常。我认为问题在于我如何更新 time_text,但我不知道如何修复它。
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import pandas as pd
box_size = 50
radius = 1
df =pd.DataFrame(np.array([np.arange(50),np.arange(50),np.arange(50)]).T,
columns = ['x','y','frame'])
#set up the figure
fig = plt.figure()
plt.axis([0,box_size,0,box_size])
ax = plt.gca()
ax.set_aspect(1)
time_text = ax.text(5, 5,'')
#initialization of animation, plot empty array of patches
def init():
time_text.set_text('initial')
return []
def animate(i):
patches = []
#data for this frame only
data = df[df.frame == i]
time_text.set_text('frame'+str(i))
#plot circles at particle positions
for idx,row in data.iterrows():
patches.append(ax.add_patch(plt.Circle((row.x,row.y),radius,color= 'b',
alpha = 0.5)))
return patches, time_text
anim = animation.FuncAnimation(fig, animate, init_func=init, repeat = False,
frames=int(df.frame.max()), interval=50,
blit=True)
【问题讨论】:
-
this question 可能会有所帮助。
标签: python-2.7 matplotlib