matplotlib 如何从数据框绘制组条形图,可视化每行的每个类别,其指标值位于每列[重复]

pxq42qpu  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(170)

此问题在此处已有答案

How to create a grouped bar plot(4个答案)
Grouped Bar graph Pandas(2个答案)
Making a clustered bar chart(4个答案)
How to create grouped bar plots in a single figure from a wide dataframe(2个答案)
seaborn bar plot from dataframe with multiple columns and datetime index(1个答案)
昨天关门了。
我有一个包含指标得分的数据框,其中的列是指标名称,行是类别。我希望条形图的x轴表示每行中的类别,y轴表示得分,条形图的颜色表示指标类型
Dataframe 看起来像这样,

  1. precision recall f1-score support
  2. anger 0.6374 0.150138 0.243032 726.0
  3. confusion 0.1666 0.006536 0.012579 153.0
  4. curiosity 0.3333 0.003521 0.006969 284.0
  5. desire 0.8750 0.084337 0.153846 83.0
  6. disgust 0.6250 0.040650 0.076336 123.0
  7. embarrass 0.0000 0.000000 0.000000 37.0
  8. fear 0.6666 0.020408 0.039604 98.0
  9. joy 0.7039 0.354877 0.471875 851.0
  10. love 0.8285 0.573657 0.677931 1154.0
  11. neutral 0.5394 0.371013 0.439655 1787.0
  12. optimism 0.7704 0.252688 0.380567 186.0
  13. pride 0.0000 0.000000 0.000000 16.0
  14. sadness 0.7165 0.240106 0.359684 379.0
  15. surprise 0.6956 0.057554 0.106312 278.0
  16. micro avg 0.6648 0.309667 0.422523 6155.0
  17. macro avg 0.5399 0.153963 0.212028 6155.0
  18. weight avg 0.6377 0.309667 0.391975 6155.0
  19. samples avg 0.3404 0.324519 0.327904 6155.0

如何在python中使用matplotlib或seaborn来获得与此类似的条形图

2sbarzqh

2sbarzqh1#

让我们使用数据的简化版本。我省略了图中的“支持”列,因为它太大,阻止了其他列的可见性。

  1. # Import libraries
  2. import pandas as pd
  3. # Read data
  4. data = pd.read_csv("confusion.csv")
  5. print(data)

输出:

  1. category precision recall f1-score support
  2. 0 anger 0.6374 0.150138 0.243032 726.0
  3. 1 confusion 0.1666 0.006536 0.012579 153.0
  4. 2 curiosity 0.3333 0.003521 0.006969 284.0
  5. 3 desire 0.8750 0.084337 0.153846 83.0

然后:

  1. # Set category column as index
  2. data.set_index('category', inplace=True)
  3. ax = df.plot.bar(rot=0)

输出:

展开查看全部

相关问题