Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i plot facet plots in pandas

This is what I have right now:

np.random.seed(1234)
test = pd.DataFrame({'week': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2],
                     'score': np.random.uniform(0, 1, 12),
                     'type': [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                     'type2': [3, 3, 4, 4, 5, 5, 3, 3, 4, 4, 5, 5]})

test.groupby(['week', 'type', 'type2']).agg('sum').unstack().plot(kind='bar')

enter image description here

How do I plot facet based on 'type'? I want two different plots, one for type = 1 and another type = 2.

like image 207
collarblind Avatar asked Apr 22 '15 01:04

collarblind


People also ask

What is a faceted plot?

Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of axes, where each subplot shows a subset of the data.

How do you plot 5 columns in a DataFrame in Python?

Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() method. To plot multiple data columns in single frame we simply have to pass the list of columns to the y argument of the plot function.

How do you plot specific columns in Python?

To plot a specific column, use the selection method of the subset data tutorial in combination with the plot() method. Hence, the plot() method works on both Series and DataFrame .


1 Answers

You need to unstack so type are columns, and then use the subplots parameter:

test.groupby(['week', 'type', 
              'type2']).agg('sum').unstack(1).plot(kind='bar', subplots=True)

Resulting plot

like image 98
JAB Avatar answered Sep 29 '22 22:09

JAB