如何在seaborn条形图轴上迭代并在条形图上显示值

2024-05-29 03:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个条形图如下。 我需要在每个栏上显示百分比值,如下所示。我想用seabron barplot来做。你知道吗

我搜索了堆栈溢出,但无法使用它们。 我的代码如下所示:

plt.figure(figsize=(10,6))
clrs = ['grey' if (x < max(df1['Score'])) else 'red' for x in df1['Score'] ]
sns.barplot(x='Languages',y='Score',data=df1,palette=clrs)

for spine in plt.gca().spines.values():
    spine.set_visible(False)


  plt.xlabel("")
  plt.ylabel("")
  frame1=plt.gca()

  frame1.axes.get_yaxis().set_ticks([])

for bar in bars:
    height = bar.get_height()
    plt.gca().text(bar.get_x() + bar.get_width()/2, bar.get_height() - 5, str(int(height)) + '%', 
             ha='center', color='w', fontsize=11)
  plt.show()

但它给出了以下错误: enter image description here

enter image description here


Tags: inforgetbarpltscoredf1条形图
1条回答
网友
1楼 · 发布于 2024-05-29 03:30:21

这个错误告诉您迭代器bar被解释为模块对象。你知道吗

尝试简单地修改循环,如下所示:

for b in bars:
    height = b.get_height()
    plt.gca().text(b.get_x() + b.get_width()/2, b.get_height() - 5, str(int(height)) + '%', 
             ha='center', color='w', fontsize=11)

相关问题 更多 >

    热门问题