我正在使用 matplotlib 的 FuncAnimation 函数对大型数据集的一部分进行动画处理:
fig = plt.figure(figsize=(15, 11.5))
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
xlim=(x1,x2), ylim=(y1, y2))
data,=ax.plot([],[],'o')
def init():
data.set_data([],[])
return data
def animate(t):
global x_pos,y_pos
data.set_data(x_pos[t],y_pos[t])
return data
ani=animation.FuncAnimation(fig,animate,frames=duration,interval=20,
init_func=init,blit=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
当我从头开始运行代码时,效果很好。但是,由于这涉及加载和预处理大型数据集,因此需要几分钟的时间,因此我希望能够仅运行一段代码来测试和制作动画。
然而,当我关闭动画并尝试再次运行它时,我只剩下一个空白图形 - 没有绘制任何点,并且永远不会调用 animate() 函数(我使用 print 语句对此进行了测试)。
我尝试清除情节和图形:
plt.clf()
fig.close()
plt.clear(figure)
Run Code Online (Sandbox Code Playgroud)
并尝试不同的数字,但结果是相同的。
如何清除动画以便可以再次运行它而无需重新运行整个脚本?
我正在尝试保留 Pandas 数据框的副本,以便我可以在保存原始数据的同时对其进行修改。但是当我修改副本时,原始数据框也会发生变化。前任:
df1=pd.DataFrame({'col1':['a','b','c','d'],'col2':[1,2,3,4]})
df1
col1 col2
a 1
b 2
c 3
d 4
df2=df1
df2['col2']=df2['col2']+1
df1
col1 col2
a 2
b 3
c 4
d 5
Run Code Online (Sandbox Code Playgroud)
我设置df2等于df1,然后当我修改时df2,df1也改变了。为什么会这样,有什么方法可以在不修改的情况下保存熊猫数据框的“备份”?