首页 > 解决方案 > 用百分比绘制条形图

问题描述

我需要一些帮助来绘制带有百分比的条形图。我需要这样的情节:

bar of overall males    bar of overall females
bar of hired males      bar of hired females
x axis: Males, Females 

我正在尝试这样的事情:

y = [139437,689]  # [Overall Males, Hired Males]
z = [82693,545]   # [Overall Females, Hired Females]
x = [Males, Females]  # on x-axis

ax = plt.subplot()
ax.bar(x-0.1, y, width=0.1, color='b')
ax.bar(x, z, width=0.1, color='g')

plt.show()

请帮我解决这里的代码。

标签: pythonmatplotlib

解决方案


您想要实现的目标可能是

import matplotlib.pyplot as plt

y = [139437, 82639]  # [Overall Males/Females]
z = [689, 545]   # [Hired Males/Females]
x = ['Males', 'Females']  # on x-axis

ax = plt.subplot()
ax.bar(x, y, width=0.5, color='b', label='all')
ax.bar(x, z, width=0.5, color='g', label='hired')
plt.legend()

plt.show()

但请注意,总体值雇用值之间的差异如此之大,根据您的目的,这种可视化可能不是最好的。


推荐阅读