[英]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,我正在尝试使用以下代码在 matplotlib 中生成 plot 条形图,
pyplot.bar(gender, ward, width, color='orange')
pyplot.bar(count, gender, width, color='tomato')
My expectation is as below which is created in excel,我的期望如下,创建于 excel,
Any suggestion will be helpful to get the same in matplotlib任何建议都有助于在 matplotlib 中获得相同的建议
You could achieve that very easy withseaborn.barplot
.您可以使用seaborn.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.如果没有 seaborn,您可以在绘制数据之前对数据进行 pivot。 Like this:像这样:
df.pivot(index='Ward', columns='Gender', values='Count').plot(kind='bar', color=['orange', 'tomato'])
plt.ylabel('Count')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.