如何绘制熊猫分类数据

时间:2020-07-08 11:32:17

标签: python pandas visualization

我有这种数据框:

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

我想展示一个条形图,其中:

y轴为年龄,x轴为动物,并且将动物分为不同颜色的相邻条形。

谢谢

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题

DropDownButton

enter image description here

答案 1 :(得分:1)

另一种简单的方法。将预期的x轴标签设置为索引和绘图。通过默认,浮点数/整数最终在y轴上

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

enter image description here