简体   繁体   English

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

[英]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 从0i+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.

相关问题 为什么 plt.cla() 只适用于其中一个地块? - Why does plt.cla() only work on one of the plots? 为什么 plt.plot 不显示图表? - Why plt.plot does not show me the graph? 即使在 matplotlib 中的 plt.cla() 之后,如何强制 xlim 永久不变 - How to force xlim unchanged permanently, even after plt.cla() in matplotlib plt.show() 不打印 plt.plot 仅 plt.scatter - plt.show() does not print plt.plot only plt.scatter 只显示最近点的图 - plot that only shows most recent points plt.plot(x,y)和plt.show()如何按照他们的方式工作? - How do plt.plot(x,y) and plt.show() work the way they do? 为什么 plt.hlines() 与 transform 一起使用时会调整 ylims,而 plt.plot() 不会? - Why does plt.hlines() adjust ylims when used with transform, but plt.plot() does not? 为什么从 class 调用多个函数只会返回最近调用的 function? - Why does calling multiple functions from a class only give the return for the most recent function that was called? 为什么matplotlib需要在plt.scatter()之前设置日志比例而不是plt.plot()? - Why does matplotlib require setting log scale before plt.scatter() but not plt.plot()? 当我尝试绘制度数分布时plt.show()将不起作用,但是当我绘制随机图时它将起作用 - plt.show() will not work when I attempt plot a degree distribution but it works when I plot a random graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM