如何绘制数据框的条形图?

时间:2020-07-15 18:56:06

标签: pandas matplotlib

我正在尝试绘制条形图,但是看起来确实很糟糕。

plt.style.use('ggplot')
x = ['High School or Below', 'College', 'Bachelor', 'Master or Above']
y = [maleDataFrame["Education"].str.contains("High School or Below").sum(),
     maleDataFrame["Education"].str.contains("College").sum(),
     maleDataFrame["Education"].str.contains("Bachelor").sum(),
     maleDataFrame["Education"].str.contains("Master or Above").sum()]
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, y, color=['blue','red','green','yellow'])
plt.xlabel("Type of Education")
plt.ylabel("Level of Education Male")
customTitle = ""
plt.title(customTitle)
plt.xticks(x_pos, x)

我该如何解决?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以重新添加定义y的行,以使用您拥有的数据进行复制。提示:根据您的要求,使用bar(x, y, width=30)修改条的宽度。

将代码修改为:

plt.style.use('ggplot')
x = ['High School \nor Below', 'College', 'Bachelor', 'Master or \nAbove']
y = [10, 20, 30, 40]

plt.bar(x, y, color=['blue','red','green','yellow'])
plt.xlabel("Type of Education")
plt.ylabel("Level of Education Male")
customTitle = ""
plt.title(customTitle)

提供:

Example plot

相关问题