[英]plt.CLA or CLF in animations - Why does it not work for me to only show most recent plot?
I want my animation only to show the most previous point, and I believe that I have to adjust something around this line: plt.gca().cla()我希望我的 animation 只显示最近的一点,我相信我必须围绕这条线进行一些调整: plt.gca().cla()
Can anyone tell me what I am doing wrong?谁能告诉我我做错了什么? In my animation, all the points stay visible, while I only want to show the most previous points.
在我的 animation 中,所有点都保持可见,而我只想显示最近的点。 Any suggestions?
有什么建议么?
This is my code:这是我的代码:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
title = 'Occ'
x = np.array(df.x)
y = np.array(df.y)
Writer = animation.writers['ffmpeg']
writer = Writer(fps = 4, bitrate = 1800)
fig = plt.figure(figsize = (12, 8))
def animate(i):
plt.gca().cla()
data = df.iloc[:int(i + 1)] # select data range
p = sns.scatterplot(x = 'x', y = 'y', hue = 'id', data = data, s = 200, alpha = 0.5)
p.tick_params(labelsize = 17)
plt.setp(p.lines, linewidth = 7)
plt.xlim(0, 500)
plt.ylim(0, 500)
plt.xlabel('X', fontsize = 20)
plt.ylabel('Y', fontsize = 20)
plt.title('Occ', fontsize = 20)
ani = matplotlib.animation.FuncAnimation(fig, animate, frames = len(df), repeat = True, blit=False)
ani.save('Occ.mp4', writer = writer)
The line线
data = df.iloc[:int(i + 1)] # select data range
select all the rows from 0
to i+1
, therefore you are showing a growing number of points at each frame. select 从
0
到i+1
的所有行,因此您在每帧显示越来越多的点。 If you want to show only the current point, you should do:如果你只想显示当前点,你应该这样做:
data = df.iloc[i] # select data range
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.