繁体   English   中英

动画中的 plt.CLA 或 CLF - 为什么只显示最新的 plot 对我不起作用?

[英]plt.CLA or CLF in animations - Why does it not work for me to only show most recent plot?

我希望我的 animation 只显示最近的一点,我相信我必须围绕这条线进行一些调整: plt.gca().cla()

谁能告诉我我做错了什么? 在我的 animation 中,所有点都保持可见,而我只想显示最近的点。 有什么建议么?

这是我的代码:

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)

线

data = df.iloc[:int(i + 1)]  # select data range

select 从0i+1的所有行,因此您在每帧显示越来越多的点。 如果你只想显示当前点,你应该这样做:

data = df.iloc[i]  # select data range

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM