我的目标是使用下面的df制作散点图的动画。我正在尝试将plot
andgroups
函数传递给该animate
函数。我试图从每个函数返回值,然后将它们传递给后续函数,但NameError
由于没有注册这些值,因此我得到了。
如果我删除plot
和groups
函数,并从全局工作区传递对象以进行动画处理,则脚本可以工作,但是我必须一直将它们写出来。而不是将其置于单独的功能中。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from matplotlib import animation
df1 = pd.DataFrame({
'Time' : [1,1,1,2,2,2,3,3,3],
'GroupA_X' : [3, 4, 5, 2, 5, 6, 1, 6, 7],
'GroupA_Y' : [2, 4, 5, 2, 5, 5, 2, 6, 5],
'GroupB_X' : [2, 5, 3, 2, 4, 2, 2, 3, 1],
'GroupB_Y' : [2, 4, 3, 3, 3, 4, 4, 2, 5],
})
def plot():
fig, ax = plt.subplots()
ax.grid(False)
xy = 0,0
Oval = mpl.patches.Ellipse(xy, 160, 130, lw = 2, edgecolor = 'black', color = 'blue', alpha = 0.2)
ax.add_patch(Oval)
return fig, ax
def groups():
plot()
Group_A = df1[['Time','GroupA_X','GroupA_Y']]
Group_B = df1[['Time','GroupB_X','GroupB_Y']]
GA_X = np.array(Group_A.groupby(['Time'])['GroupA_X'].apply(list))
GA_Y = np.array(Group_A.groupby(['Time'])['GroupA_Y'].apply(list))
GB_X = np.array(Group_B.groupby(['Time'])['GroupB_X'].apply(list))
GB_Y = np.array(Group_B.groupby(['Time'])['GroupB_Y'].apply(list))
GA = ax.scatter(GA_X[0], GA_Y[0], c = ['blue'], marker = 'o', s = 10, edgecolor = 'black')
GB = ax.scatter(GB_X[0], GB_Y[0], c = ['brown'], marker = 'o', s = 10, edgecolor = 'black')
return GA, GB
def animate(i) :
plot()
groups()
GA.set_offsets(np.c_[GA_X[0+i], GA_Y[0+i]])
GB.set_offsets(np.c_[GB_X[0+i], GB_Y[0+i]])
plot()
groups()
ani = animation.FuncAnimation(fig, animate, np.arange(0,3), interval = 1000, blit = False)
错误输出:
GA = ax.scatter(GA_X[0], GA_Y[0], c = ['blue'], marker = 'o', s = 10, edgecolor = 'black') NameError: name 'ax' is not defined
动画在global上绘制/重复figure
,因此您需要subplots
在global范围内创建。如果您定义subplots
内部plot
函数,则每次调用plot
都会创建一个新的subplots
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from matplotlib import animation
df1 = pd.DataFrame({
'Time' : [1,1,1,2,2,2,3,3,3],
'GroupA_X' : [3, 4, 5, 2, 5, 16, 21, 36, 47],
'GroupA_Y' : [2, 4, 5, 2, 5, 15, 22, 36, 45],
'GroupB_X' : [2, 5, 3, 2, 4, 12, 22, 33, 41],
'GroupB_Y' : [2, 4, 3, 3, 3, 14, 24, 32, 45],
})
fig, ax = plt.subplots()
def plot():
# fig, ax = plt.subplots() #declared in global scope
ax.grid(False)
xy = 0,0
Oval = mpl.patches.Ellipse(xy, 160, 130, lw = 2, edgecolor = 'black', color = 'blue', alpha = 0.2)
ax.add_patch(Oval)
# return fig, ax #no need return since `fig, ax` are in global scope
def groups():
# plot() #no need since this function use nothing from `plot`
Group_A = df1[['Time','GroupA_X','GroupA_Y']]
Group_B = df1[['Time','GroupB_X','GroupB_Y']]
GA_X = np.array(Group_A.groupby(['Time'])['GroupA_X'].apply(list))
GA_Y = np.array(Group_A.groupby(['Time'])['GroupA_Y'].apply(list))
GB_X = np.array(Group_B.groupby(['Time'])['GroupB_X'].apply(list))
GB_Y = np.array(Group_B.groupby(['Time'])['GroupB_Y'].apply(list))
GA = ax.scatter(GA_X[0], GA_Y[0], c = ['blue'], marker = 'o', s = 10, edgecolor = 'black')
GB = ax.scatter(GB_X[0], GB_Y[0], c = ['brown'], marker = 'o', s = 10, edgecolor = 'black')
return GA, GB, GA_X, GA_Y, GB_X, GB_Y
def animate(i) :
# plot()
GA, GB, GA_X, GA_Y, GB_X, GB_Y = groups()
GA.set_offsets(np.c_[GA_X[0+i], GA_Y[0+i]])
GB.set_offsets(np.c_[GB_X[0+i], GB_Y[0+i]])
plot()
# groups()
ani = animation.FuncAnimation(fig, animate, np.arange(0,3), interval = 1000, blit = False)
注意:我更改了值,df1
以使每个动画之间的值都更清晰。修复以上工作的代码。我测试了 但是,我不知道它是否有效。我基本上只是修复您的代码以使其运行。
您说如果在全局范围内声明所有内容,动画运行就很好。因此,我假设您的系统已经ffmpeg
安装并且您的代码能够调用/查找ffmpeg.exe
以显示重新定义/循环动画
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句