0

I thought this would turn out easy, but I am struggling now for a few hours to animate my seaborn scatter plots iterating over my datetime values.

The x and y variables are coordinates, and I would like to animate them according to the datetime variable, colored by their "id".

My data set looks like this:

df.head(10)
Out[64]: 
                     date    id    x    y  
0 2019-10-09 15:20:01.418  3479  353  118  
1 2019-10-09 15:20:01.418  3477  315   92  
2 2019-10-09 15:20:01.418  3473  351  176     
3 2019-10-09 15:20:01.418  3476  318  176     
4 2019-10-09 15:20:01.418  3386  148  255     
5 2019-10-09 15:20:01.418  3390  146  118     
6 2019-10-09 15:20:01.418  3447  469  167     
7 2019-10-09 15:20:03.898  3476  318  178     
8 2019-10-09 15:20:03.898  3479  357  117     
9 2019-10-09 15:20:03.898  3386  144  257     

The plot that should be iterated looks like this:

enter image description here.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Have you tried `matplotlib.animation`? There are two explainations: https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ https://salernoali.medium.com/making-an-animated-graph-with-matplotlib-seaborn-3f865fa8c16a – samusa Jun 30 '21 at 10:04

1 Answers1

0

Below is a quick example. You might want to fix the axes limits to make the transitions nicer.

import pandas as pd
import seaborn as sns
import matplotlib.animation 
import matplotlib.pyplot as plt

def animate(date):
    df2 = df.query('date == @date')
    ax = plt.gca()
    ax.clear()
    return sns.scatterplot(data=df2, x='x', y='y', hue='id', ax=ax)

fig, ax = plt.subplots()
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=df.date.unique(), interval=100, repeat=True)
plt.show()

NB. I assumed that date is sorted in the order of the frames

edit: If using a Jupyter notebook, you should wrap the animation to display it. See for example this post.

from matplotlib import animation
from IPython.display import HTML
import matplotlib.pyplot as plt
import seaborn as sns

xmin, xmax = df.x.agg(['min', 'max'])
ymin, ymax = df.y.agg(['min', 'max'])

def animate(date):
    df2 = df.query('date == @date')
    ax = plt.gca()
    ax.clear() # needed only to keep the points of the current frame
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)
    return sns.scatterplot(data=df2, x='x', y='y', hue='id', ax=ax)
    
fig, ax = plt.subplots()

anim = animation.FuncAnimation(fig, animate, frames=df.date.unique(), interval=100, repeat=True)

HTML(anim.to_html5_video())
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thank you so much mozway, but is it possible my plots are blank after running this? Could you cross-check? –  Jun 30 '21 at 10:35
  • Are you running the code in a shell / as a script? – mozway Jun 30 '21 at 11:41
  • I use Spyder, its needed to use jupyter? - is it maybe possible to directly safe the animation to avoid a browser solution? –  Jun 30 '21 at 11:45
  • If you use Jupyter, you should wrap your animation for it to display, see my edit above. – mozway Jun 30 '21 at 12:39