使用Seaborn relplot和赛璐珞制作动画

2024-10-02 10:26:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试制作动画时使用Seaborn relplot时遇到问题。我使用Seaborn附带的一个数据集重新创建了这个问题,如下所示

我怀疑这与plt.figure()sns.relplot不同有关。任何关于如何使这项工作的想法都会被广泛接受。谢谢

%matplotlib inline
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt
from celluloid import Camera
from IPython.display import HTML
import ffmpeg

df = sns.load_dataset('car_crashes')

f = plt.figure(figsize=(3,3))
camera = Camera(f)

# This might seem a little bit unnecessary, but its emulating the way I am plotting my other data source:
for i in range(0, len(df), 10):
    g = sns.relplot(x='total', y='abbrev', hue='abbrev', data=df.iloc[i:i+10 , [0,7]])                                                                        
    plt.axis('off')
    plt.title(f'THIS IS THE TITLE OF {i}')
    plt.gca().set_aspect('equal')
    camera.snap()
    
animation = camera.animate()
HTML(animation.to_html5_video())

Tags: fromimportdfdatamatplotlibhtmlasplt

热门问题