Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Pandas groupby plot with subplots

I have a data frame like this:

     value     identifier
2007-01-01  0.781611      55
2007-01-01  0.766152      56
2007-01-01  0.766152      57
2007-02-01  0.705615      55
2007-02-01  0.032134      56 
2007-02-01  0.032134      57
2008-01-01  0.026512      55
2008-01-01  0.993124      56
2008-01-01  0.993124      57
2008-02-01  0.226420      55
2008-02-01  0.033860      56
2008-02-01  0.033860      57

So I do a groupby per identifier:

df.groupby('identifier')

And now I want to generate subplots in a grid, one plot per group. I tried both

df.groupby('identifier').plot(subplots=True)

or

df.groupby('identifier').plot(subplots=False)

and

plt.subplots(3,3)
df.groupby('identifier').plot(subplots=True)

to no avail. How can I create the graphs?

like image 730
Ivan Avatar asked Apr 30 '15 19:04

Ivan


2 Answers

Here's an automated layout with lots of groups (of random fake data) and playing around with grouped.get_group(key) will show you how to do more elegant plots.

import pandas as pd
from numpy.random import randint
import matplotlib.pyplot as plt


df = pd.DataFrame(randint(0,10,(200,6)),columns=list('abcdef'))
grouped = df.groupby('a')
rowlength = grouped.ngroups/2                         # fix up if odd number of groups
fig, axs = plt.subplots(figsize=(9,4), 
                        nrows=2, ncols=rowlength,     # fix as above
                        gridspec_kw=dict(hspace=0.4)) # Much control of gridspec

targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
    ax.plot(grouped.get_group(key))
    ax.set_title('a=%d'%key)
ax.legend()
plt.show()

enter image description here

like image 104
cphlewis Avatar answered Oct 12 '22 20:10

cphlewis


If you have a series with multiindex. Here's another solution for the wanted graph.

df.unstack('indentifier').plot.line(subplots=True)
like image 38
Gabriel_F Avatar answered Oct 12 '22 20:10

Gabriel_F