Matplotlib: how to plot categorical data on the y-axis?

我只是一个虾纸丫 提交于 2019-12-01 14:38:15

Perhaps you're looking for barh:

gender = ['male','male','female','male','female']

import matplotlib.pyplot as plt
from collections import Counter

c = Counter(gender)

men = c['male']
women = c['female']

bar_heights = (men, women)
y = (1, 2)

fig, ax = plt.subplots()
width = 0.4

ax.barh(y, bar_heights, width)

ax.set_ylim((0, 3))
ax.set_xlim((0, max(men, women)*1.1))

ax.set_yticks([i+width/2 for i in y])
ax.set_yticklabels(['male', 'female'])

plt.show()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!