首页 > 解决方案 > 使用 Seaborn relplot 和赛璐珞的动画

问题描述

尝试制作动画时,我在使用 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())

标签: pythonmatplotlibanimationseaborn

解决方案


推荐阅读