Reputation: 153
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 :)
Upvotes: 1
Views: 252
Reputation: 26686
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)
Upvotes: 1