简体   繁体   中英

Plotting Bar Chart with X, Y and Z axis in matplotlib

I have below data,

在此处输入图像描述

Am trying to plot bar chart in matplotlib using below code,

    pyplot.bar(gender, ward, width, color='orange')
    pyplot.bar(count, gender, width, color='tomato')

Below is the result, 在此处输入图像描述

My expectation is as below which is created in excel,

在此处输入图像描述

Any suggestion will be helpful to get the same in matplotlib

You could achieve that very easy withseaborn.barplot .

import seaborn as sns
sns.barplot(data=df, x='Ward', y='Count', hue='Gender', palette=['orange', 'tomato']) #df is the dataframe you showed as example

在此处输入图像描述

Without seaborn, you could pivot your data before plotting it. Like this:

df.pivot(index='Ward', columns='Gender', values='Count').plot(kind='bar', color=['orange', 'tomato'])
plt.ylabel('Count')

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