简体   繁体   English

在 matplotlib 中绘制带有 X、Y 和 Z 轴的条形图

[英]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')

Below is the result,下面是结果, 在此处输入图像描述

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.

相关问题 使用不带参数形式的 matplotlib 绘制 x^2 + y^2 = z^2 = 1 - Plotting x^2 + y^2 = z^2 = 1 using matplotlib without parametric form 绘制 DataFrame x,y,z(数据透视表)- 切换轴 - Plotting DataFrame x,y,z (Pivot-Table) - Switch axis Matplotlib:一个 X 轴,带有两个不同类型(整数和字符串)的 x 标签,条形图中带有一个 Y 轴 - Matplotlib: One X-Axis with two x labels of different type (int and string) in bar chart with one Y-Axis 在 matplotlib 上绘制条形图 - Plotting a Bar Chart on matplotlib 使用matplotlib在熊猫中以对数比例绘制x和y轴 - plotting both x and y axis in log scale in pandas using matplotlib 将数据框绘制为条形图,每列在单独的 y 轴上 - Plotting Dataframe as a bar chart with each column on a separate y axis Matplotlib 不会用条形图显示所有刻度/标签。 它跳过 y 轴的第一个和 x 轴的最后一个 - Matplotlib don't show all ticks/labels with bar chart. It skips first of y axis and last one of x axis 如何使用 matplotlib.pyplot 在 x 轴上创建分组条形图(按月和年)并在 y 轴上创建值? - How to create a grouped bar chart (by month and year) on the x-axis and values on the y-axis using matplotlib.pyplot? 使用matplotlib绘制带有纪元时间x轴的图表 - Plotting chart with epoch time x axis using matplotlib 堆叠条形图交换 x 轴和 y 轴 - Stacked bar chart swap x-axis and y-axis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM