繁体   English   中英

如何 pandas 分类数据中的 plot

[英]how to plot in pandas categorical data

我有这种dataframe:

  animal  age    where
0    dog    1   indoor
1    cat    4   indoor
2  horse    3  outdoor

我想介绍一个酒吧 plot 其中:

y 轴是年龄,x 轴是动物,动物被分组在不同的 colors 的相邻条中。

谢谢

这应该可以解决问题


df = pd.DataFrame({"animal":["dog","cat","horse"],"age":[1,4,3],"where":["indoor","indoor","outdoor"]})


df
animal  age where
0   dog 1   indoor
1   cat 4   indoor
2   horse   3   outdoor

ax = df.plot.bar(x="animal",y="age",color=["b","r","g"])
ax.legend("")
ax.set_ylabel("Age")

在此处输入图像描述

另一种简单的方法。 将预期的 x 轴 label 设置为索引和 plot。 默认情况下,浮点数/整数在 y 轴上结束

import matplotlib.pyplot as plt
df.set_index(df['animal']).plot( kind='bar')
plt.ylabel('age')

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM