假设我有以下代码,它来自here https://stackoverflow.com/questions/29508208/best-way-to-plot-categorical-data?answertab=active#tab-top:
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)
x = (1, 2)
fig, ax = plt.subplots()
width = 0.4
ax.bar(x, bar_heights, width)
ax.set_xlim((0, 3))
ax.set_ylim((0, max(men, women)*1.1))
ax.set_xticks([i+width/2 for i in x])
ax.set_xticklabels(['male', 'female'])
plt.show()
怎么可以分类别male
and female
绘制在 y 轴上,而不是 x 轴上?
也许您正在寻找barh http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)