【问题标题】:How to annotate horizontal Seaborn countplots?如何注释水平 Seaborn 计数图?
【发布时间】:2018-05-05 14:16:51
【问题描述】:

如何操作注释的位置,以便它们显示在条形的末尾?

这是我目前所得到的:https://i.imgur.com/5NEzoVY.png

还有,代码如下:

fig = plt.figure(1, figsize=(15,10))
ax = sns.countplot(y="carrier", hue='Delay_Level', data=Subset_Data)
plt.xlabel('count', fontsize=15, weight = 'bold', labelpad=10)
plt.ylabel('ABC', fontsize=15, weight = 'bold', labelpad=10)

for p in ax.patches:
    x=p.get_bbox().get_points()[:,1]
    y=p.get_bbox().get_points()[1,0]
    ax.annotate(y,(p.get_height()+2,p.get_y()+0.15))

如果有人可以帮助我,将不胜感激。我是初学者。我浏览了文档,但找不到帮助。 谢谢!

【问题讨论】:

    标签: python-3.x plot seaborn


    【解决方案1】:

    更新:

    我找到了解决方案:

    for p in ax.patches:
        ax.annotate(int(p.get_width()),((p.get_x() + p.get_width()), p.get_y()), xytext=(1, -18),fontsize=9,color='#004d00',textcoords='offset points', horizontalalignment='right')
    

    我没有使用边界框的点,而是使用 get_width 来获取矩形的宽度。 “textcoords”有助于将注释放在各自的栏旁边;它指示 xytext 中使用的坐标类型。将“horizo​​ntalalignment”设置为“right”会使它们靠近条形图的末端。还为注释添加了“fontsize”和“color”。

    【讨论】:

    • 请问((p.get_x() + p.get_width())是什么?
    猜你喜欢
    • 2020-03-31
    • 2019-01-31
    • 2022-01-05
    • 2020-11-22
    • 2017-09-30
    • 2017-08-09
    • 2015-12-05
    • 2020-08-10
    • 2016-03-21
    相关资源
    最近更新 更多