Matplotlib:如何在y轴上绘制分类数据? [英] Matplotlib: how to plot categorical data on the y-axis?

查看:86
本文介绍了Matplotlib:如何在y轴上绘制分类数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码,这些代码来自


Let's say that I have the following code, which comes from here:

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()

How could the categories male and female be plotted on the y-axis, as opposed to the x-axis?

解决方案

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()

这篇关于Matplotlib:如何在y轴上绘制分类数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆