如何使用子图创建Pandas groupby图?

时间:2015-04-30 19:10:18

标签: python pandas plot group-by subplot

我有一个这样的数据框:

     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

所以我按标识符进行分组:

df.groupby('identifier')

现在我想在网格中生成子图,每组一个图。我试过了两个

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

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

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

无济于事。如何创建图表?

4 个答案:

答案 0 :(得分:9)

这是一个自动布局,包含大量群组(随机假数据),并且使用grouped.get_group(key)进行游戏,将向您展示如何制作更优雅的地块。

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

答案 1 :(得分:8)

您确实使用pivot来获取列中的identifiers,然后绘制

pd.pivot_table(df.reset_index(),
               index='index', columns='identifier', values='value'
              ).plot(subplots=True)

enter image description here

而且,

的输出
pd.pivot_table(df.reset_index(),
               index='index', columns='identifier', values='value'
               )

看起来像 -

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

答案 2 :(得分:0)

如果您的系列有多重索引。这是通缉图的另一种解决方案。

df.unstack('indentifier').plot.line(subplots=True)

答案 3 :(得分:0)

对于那些需要绘制图形以通过多列分组探索不同级别的聚合的人,这是一个解决方案。

from numpy.random import randint
from numpy.random import randint
import matplotlib.pyplot as plt
import numpy as np

levels_bool = np.tile(np.arange(0,2), 100)
levels_groups = np.repeat(np.arange(0,4), 50)
x_axis = np.tile(np.arange(0,10), 20)
values = randint(0,10,200)

stacked = np.stack((levels_bool, levels_groups, x_axis, values), axis=0)
df = pd.DataFrame(stacked.T, columns=['bool', 'groups', 'x_axis', 'values'])

columns = len(df['bool'].unique())
rows = len(df['groups'].unique())
fig, axs = plt.subplots(rows, columns, figsize = (20,20))

y_index_counter = count(0)
groupped_df = df.groupby([ 'groups', 'bool','x_axis']).agg({
    'values': ['min', 'mean', 'median', 'max']
})
for group_name, grp in groupped_df.groupby(['groups']):
    y_index = next(y_index_counter)
    x_index_counter = count(0)
    for boolean, grp2 in grp.groupby(['bool']):
        x_index = next(x_index_counter)
        axs[y_index, x_index].plot(grp2.reset_index()['x_axis'], grp2.reset_index()['values'], 
                                   label=str(key)+str(key2))
        axs[y_index, x_index].set_title("Group:{} Bool:{}".format(group_name, boolean))

ax.legend()
plt.subplots_adjust(hspace=0.5)
plt.show()

groupped by pandas plot