Parthiban Rajendran
Parthiban Rajendran

Reputation: 450

How to funcanimation seaborn's lmplot

(This is an extension of this question)

Hi

I am trying to update lmplot with new data but unable to find a way to hook it up to existing figure/axes since they have their own. I have tried so far like this:

%matplotlib notebook

import matplotlib.animation as animation
import numpy as np

#fig, ax = plt.subplots(1,1,figsize=(5,4))

df = get_data()
g = sns.lmplot( x='Mean', y='Variance', data=df, fit_reg=False, hue='Size', legend=False, palette=cmap)

def get_data():
    takeRandomSample(population, popSize, popVar)   
    current_elem = len(sampleStats)-1
    current_size = sampleStats[current_elem][0]
    current_mean = sampleStats[current_elem][1]
    current_var =  sampleStats[current_elem][2]

    data = {'Size' : current_size, 'Mean' : current_mean, 'Variance' : current_var}
    df = pd.DataFrame(data, index=[0])

    return df


def prep_axes(g):
    g.set(xlim=(0, 20), ylim=(0, 100), xticks=range(0,21))    

    ax = g.axes[0,0]       
    ax.axvline(x=popMean, color='#8c8ca0', ls='dashed')
    ax.axhline(y=popVar, color='#8c8ca0', ls='dashed')
    ax.set_title('Sample Statistics :{}'.format(i))
    ax.set_facecolor(backgroundColour)

def animate(i):
    df = get_data()
    g = sns.lmplot( x='Mean', y='Variance', data=df, fit_reg=False, hue='Size', legend=False, palette=cmap)
    prep_axes(g, i)


# initialize samples
sampleStats = []

plt.tight_layout()

ani = animation.FuncAnimation(g.fig, animate, frames = np.arange(1,100), interval=100)

Output:
enter image description here

Problems:
1. This just produces one static graph, because I Could not find a way to update existing g, and re plot on same g's figure, the lmsplot. animate function is creating new g
2. I had to initialize unnecessarily once to get g object to pass on g.fig to funcanimation which anyway did not help due to point 1 as well.

How do we animate using lmplot? I want to use this instead of regular matplotlib due to hue feature.

I tried using facetgrid directly as well (and pass on lmplot in g.map), but that did not help either.

Upvotes: 0

Views: 1719

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40707

For the record, here is how you could animate an lmplot(), provided you don't have any facets and you don't care about regressions:

import seaborn as sns; sns.set(color_codes=True)
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, fit_reg=False)
fig = g.fig
ax = g.axes[0,0]
scatters = [c for c in ax.collections if isinstance(c, matplotlib.collections.PathCollection)]
txt = ax.text(0.1,0.9,'frame=0', transform=ax.transAxes)

def animate(i):
    for c in scatters:
        # do whatever do get the new data to plot
        x = np.random.random(size=(50,1))*50
        y = np.random.random(size=(50,1))*10
        xy = np.hstack([x,y])
        # update PathCollection offsets
        c.set_offsets(xy)
    txt.set_text('frame={:d}'.format(i))
    return scatters+[txt]

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=10, blit=True)

enter image description here

However in this case, you can obtain almost exactly the same result, in a much more straightforward way, by not using lmplot altogether:

import seaborn as sns; sns.set(color_codes=True)
tips = sns.load_dataset("tips")

fig, ax = plt.subplots()
scatters = []
for g,d in tips.groupby('smoker'):
    s = ax.scatter(x="total_bill", y="tip", data=tips, label=g)
    scatters.append(s)
ax.legend(bbox_to_anchor=(1.,1.), loc=1)
txt = ax.text(0.1,0.9,'frame=0', transform=ax.transAxes)

def animate(i):
    for c in scatters:
        x = np.random.random(size=(50,1))*50
        y = np.random.random(size=(50,1))*10
        xy = np.hstack([x,y])
        c.set_offsets(xy)
    txt.set_text('frame={:d}'.format(i))
    return scatters+[txt]

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=10, blit=True)

enter image description here

From the code above, it is fairly simple to append new values to the previous data, instead of replacing all the points:

import seaborn as sns; sns.set(color_codes=True)
tips = sns.load_dataset("tips")

fig, ax = plt.subplots()
scatters = []
for g,d in tips.groupby('smoker'):
    s = ax.scatter([], [], label=g)
    scatters.append(s)
ax.legend(bbox_to_anchor=(1.,1.), loc=1)
txt = ax.text(0.1,0.9,'frame=0', transform=ax.transAxes)
ax.set_xlim((0,60))
ax.set_ylim((0,15))

def animate(i, df, x, y, hue):
    new_data = df.sample(20) # get new data here
    for c,(groupname,subgroup) in zip(scatters,new_data.groupby(hue)):
        xy = c.get_offsets()
        xy = np.append(xy,subgroup[[x,y]].values, axis=0)
        c.set_offsets(xy)
    txt.set_text('frame={:d}'.format(i))
    return scatters+[txt]

ani = matplotlib.animation.FuncAnimation(fig, animate, fargs=(tips, "total_bill", "tip", 'smoker'), frames=10, blit=True)

enter image description here

Upvotes: 3

Related Questions