简体   繁体   中英

How to make a stacked bar chart in matplotlib?

Here is some code I am working with: (the data is made up)

# create dataset
mydf = {'Person': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'],
        'Punctuality':  ['On Time', 'Late', 'On Time', 'Early', 'On Time', 'Early',
                     'Late', 'Early', 'Early', 'On Time', 'On Time', 'Late'],
        'Diverted': [1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
       }
# note: in 'Diverted', 1 = True, 0 = False

mydf = pd.DataFrame (mydf, columns = ['Person', 'Punctuality', 'Diverted'], index = list(range(0,12)))
mydf
# create freq table
mydf_p = (pd.DataFrame(mydf['Punctuality'].value_counts())).reset_index()
mydf_p.rename(columns={ mydf_p.columns[0]: "Punctuality", mydf_p.columns[1]: "Frequency" }, inplace = True)
mydf_p
# create bar chart
plot = plt.subplots(nrows=1, ncols=1, figsize=(4, 5))
fig, (ax) = plot
x = mydf_p['Punctuality'].to_list()
y = mydf_p['Frequency'].to_list()

ax.bar(x, y, color='cornflowerblue')
ax.set_ylabel('Frequency')
ax.set_xlabel('Punctuality')

在此处输入图像描述

How can I make a stacked bar chart using this data where the punctuality bars are split in proportion of people's journey being diverted or not??

I tried to follow the documentation but I'm still quite confused on how to do this. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html

Really hope someone can help:)

Your question isnt as clear. Maybe expected output would be helpful. If I got it right, please try groupby, unstack and plot.

mydf.groupby(['Punctuality','Diverted']).Person.count().unstack().plot.bar(stacked=True)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM