Pineapple P
Pineapple P

Reputation: 83

Stacked Bar Chart matplotlib or seaborn

I have the following dataframe:

import pandas as pd

data = {'country': ['US', 'DE', 'IT', 'US', 'DE', 'IT', 'US', 'DE', 'IT'],
        'year': [2000,2000,2000,2001,2001,2001,2002,2002,2002],
        'share': [0.5, 0.3, 0.2, 0.6,0.1,0.3,0.4,0.2,0.4]}

data = pd.DataFrame(data)

I want to display the data with a stacked bar chart.

X-axis: year, Y-axis: share, Color: country

All the three bars for 2000, 2001 and 2002 should have the same height (for each year, the total of the share == 1)

Upvotes: 0

Views: 57

Answers (1)

mozway
mozway

Reputation: 260630

You can use a pivot and plot.bar with stacked=True:

data.pivot('year', 'country', 'share').plot.bar(stacked=True)

output:

enter image description here

Upvotes: 1

Related Questions