2017-06-27 9 views
1

Dies funktioniert, aber ist es nicht eine bessere Möglichkeit, Facette Barplot mit Bars sind Seite an Seite in Pandas facettieren? Ich habe das Gefühl, dass es eine saubere Möglichkeit gibt, mit Multiindex und Subplots zu schreiben.Facet Barplot mit Bars sind Seite an Seite in Pandas

import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.DataFrame({'Group': ['A','A','B','B'], 
        'Name':['name1','name2','name3','name4'], 
        '2016': [1,2,3,4], 
        '2017': [1.2,2.1,3.0,4.9]})  
df.set_index(['Name'], inplace=True) 

fig, ax = plt.subplots(2) 

group_a = df[df.Group=='A'] 
group_b = df[df.Group=='B'] 
group_a.plot(kind='bar', rot=0, ax=ax[0]) 
group_b.plot(kind='bar', rot=0, ax=ax[1]) 

enter image description here

Antwort

2

Wenn Sie etwas mehr als einmal tun wollen, ist es angesichts immer lohnt sich die Verwendung einer Funktion und einer Schleife. Hier ist eine Schleife ausreichend.

groups = df.Group.unique() 

fig, ax = plt.subplots(len(groups)) 

for i, group in enumerate(groups): 
    df[df.Group==group].plot(kind='bar', rot=0, ax=ax[i]) 
Verwandte Themen