加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何使用Pandas绘制条形图?

发布时间:2020-12-20 11:59:08 所属栏目:Python 来源:网络整理
导读:我有像这样的pandas数据框 a b c d e f label1 3 4 5 6 7 12 2 5 7 5 7 04 7 9 0 8 7 16 9 4 7 3 8 17 0 9 8 7 6 0 我想要一个看起来像这样的条形图 – : 我已经尝试过使用pandas中的hist()函数,但是我无法弄清楚如何在条形图中包含标签以获得如图中的图形.
我有像这样的pandas数据框

a  b  c  d  e  f  label
1  3  4  5  6  7    1
2  2  5  7  5  7    0
4  7  9  0  8  7    1
6  9  4  7  3  8    1
7  0  9  8  7  6    0

我想要一个看起来像这样的条形图 – :

enter image description here

我已经尝试过使用pandas中的hist()函数,但是我无法弄清楚如何在条形图中包含标签以获得如图中的图形.

解决方法

我认为 cumcount需要 pivot
并最后拨打 DataFrame.plot.bar

df = pd.pivot(index=df.groupby('label').cumcount(),columns=df.label,values=df.a).fillna(0)
print (df)
label    0    1
0      2.0  1.0
1      7.0  4.0
2      0.0  6.0

df.plot.bar()

graph1

或者可能需要在unstack之前重新组合size

df = df.groupby(['label','a']).size().unstack(0,fill_value=0)

df.plot.bar()

使用piRSquared数据获得更好的样本:

graph2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读