Схема расположения Panda Groupby - PullRequest
0 голосов
/ 05 мая 2020

У меня 69 машин, и у каждой машины есть данные о производстве за 12 месяцев.

Я рисую их все с помощью groupby.plot () и получил длинный список просмотров. Не знаете, как сделать компактный макет, чтобы я мог сразу их просмотреть? Требуемый результат: каждая строка содержит 7 столбцов и 69/7 строк. Пожалуйста, помогите!

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

enter image description here

Ответы [ 2 ]

1 голос
/ 05 мая 2020

Я подумал, что добавлю пример использования seaborn, поскольку он может быть полезен в этом контексте, поскольку с ним довольно легко обернуть вещи по столбцам. Я ожидаю, что найдется кто-то, кто сможет дать более хороший ответ, возможно, используя pandas, и я надеюсь, что да.

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)

enter image description here

1 голос
/ 05 мая 2020

Вот мой окончательный ответ.

# 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()
...