簡體   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