如何使用子图创建 Pandas groupby 图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29975835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Pandas groupby plot with subplots?
提问by Ivan
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?
无济于事。如何创建图表?
回答by cphlewis
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.
这是一个包含大量组(随机假数据)的自动布局,并且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()


回答by Zero
You do use pivot to get the identifiersin columns and then plot
您确实使用 pivot 来获取identifiers列,然后绘制
pd.pivot_table(df.reset_index(),
index='index', columns='identifier', values='value'
).plot(subplots=True)


And, the output of
并且,输出
pd.pivot_table(df.reset_index(),
index='index', columns='identifier', values='value'
)
Looks like -
好像 -
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
回答by Gabriel_F
If you have a series with multiindex. Here's another solution for the wanted graph.
如果您有一个带有多索引的系列。这是想要的图表的另一种解决方案。
df.unstack('indentifier').plot.line(subplots=True)
回答by beyondfloatingpoint
Here is a solution to those, who need to plot graphs for exploring different levels of aggregation by multiple columns grouping.
这是针对那些需要绘制图表以通过多列分组探索不同聚合级别的人的解决方案。
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()

