简体   繁体   中英

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()

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. 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. If you want to show only the current point, you should do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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