【问题标题】:Pandas Groupby Plot LayoutPandas Groupby 剧情布局
【发布时间】:2020-08-19 11:14:29
【问题描述】:

我有 69 台机器,每台机器都有 12 个月的生产数据。

我用 groupby.plot() 将它们全部绘制出来,并得到了一长串视图。想知道如何制作紧凑的布局以便我可以立即查看它们?想要的结果是每行有 7 列和 69/7 行。请帮忙!

c1.groupby('System ID').plot(x='Month', y='Monthly Production',kind='bar',legend=True)

【问题讨论】:

  • 示例数据在这里会有所帮助 - 但您可能想查看sns.catplot,它具有参数colcol_wrap,这将使您能够做您想做的事情
  • @baxx 谢谢。向jonathansoma.com/lede/data-studio/classes/small-multiples/…学习了一番,终于成功了!!!
  • 哦,你是在纯 matplotlib 中完成的,太好了!感谢您的链接,我发布的内容非常适合能够快速生成内容......我将添加一个快速版本,我不希望它是一个答案,只要它感兴趣。另外 - 如果您能够在您给出的答案中提供任何数据,这对其他认为可以从中学习的人很有用

标签: python pandas matplotlib pandas-groupby


【解决方案1】:

我想我会添加一个使用 seaborn 的示例,因为它在这种情况下可能很有用,因为它很容易按列包装内容。我希望有人可以提供更好的答案,也许使用熊猫,我希望他们这样做。

import seaborn as sns
import pandas as pd
import numpy as np

np.random.seed(1)

N = 2000

df = pd.DataFrame(np.random.randint(0,4, (N,7)))
df['system'] = np.random.randint(0, 69, N )

这给了df

      0  1  2  3  4  5  6  system
674   1  2  3  1  0  0  0      15
1699  0  0  1  3  0  0  1       9
1282  0  0  0  0  1  0  2      47
1315  0  3  1  3  1  1  1      37
1210  1  1  0  3  1  3  1      11

在绘图前融化数据:

df_plot = df.melt(id_vars='system')

看起来像


       system variable  value
8756       23        4      2
5474       24        2      2
11242      12        5      2
7820       56        3      3

然后

sns.catplot(x = 'variable', y = 'value', col = 'system', 
    hue = 'variable', dodge = False,
    col_wrap = 6, data = df_plot, kind = 'bar', ci = False)

【讨论】:

  • 哇,看看你的代码,我觉得我在我的代码上浪费了太多时间!感谢分享!
【解决方案2】:

这是我的最终答案。

# We can ask for ALL THE AXES and put them into axes
fig, axes = plt.subplots(nrows=10, ncols=7, sharex=True, sharey=False, figsize=(20,15))
axes_list = [item for sublist in axes for item in sublist] 

ordered_systems = grouped['Monthly Production'].last().sort_values(ascending=False).index

# Now instead of looping through the groupby
# you CREATE the groupby
# you LOOP through the ordered names
# and you use .get_group to get the right group
grouped = c1.groupby("System ID")

first_month = c1['Month'].min()
last_month = c1['Month'].max()

for system in ordered_systems:
    selection = grouped.get_group(system)

    ax = axes_list.pop(0)
    selection.plot(x='Month', y='Monthly Production', label=system, ax=ax, legend=False)
    selection.plot(x='Month', y='Monthly Usage',secondary_y=True, ax=ax, legend=False)
    ax.set_title(system)
    ax.tick_params(
        which='both',
        bottom='off',
        left='off',
        right='off',
        top='off'
    )
    ax.grid(linewidth=0.25)
    ax.set_xlim((first_month, last_month))
    ax.set_xlabel("")
    ax.set_xticks((first_month, last_month))
    ax.spines['left'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)

# Now use the matplotlib .remove() method to 
# delete anything we didn't use
for ax in axes_list:
    ax.remove()

plt.subplots_adjust(hspace=1)

plt.tight_layout()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多