rienverbeke
rienverbeke

Reputation: 13

pandas plotting a group

I want to plot the following data which is a count of unique text

Element              
Motor 1  thermiek        15
         tijd te lang     9
Motor 2  thermiek        12
         tijd te lang     3
Motor 3  thermiek         5
         tijd te lang     4
dtype: int64

by_element = data.groupby('Element')
by_element['Alarm tekst'].value_counts().plot(kind='bar')

result of code

enter image description here

How can I make the plot be grouped like:

enter image description here

Upvotes: 1

Views: 114

Answers (1)

Daniel Marasco
Daniel Marasco

Reputation: 96

This should work to get a grouped bar chart similar to the one linked in your comment:

gb = df.groupby(['Element','Alarm tekst'])
gb['Alarm tekst'].count().unstack().plot(kind = 'bar')

Original suggestion for aggregated bar:

You should include the agg() function to count the total values.

data.groupby('Element').agg('count').plot(kind = 'bar')

If your second column is already summed by term you can use agg(numpy.sum) instead:

import numpy    
data.groupby('Element').agg(numpy.sum).plot(kind = 'bar')

Upvotes: 1

Related Questions