SNS条形图颜色强度错误

2024-03-29 07:37:52 发布

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

我正在使用以下代码使用SNS绘制以下类型的条形图。我使用了cubehelix_palette,因为我想要根据值的条形颜色强度。我希望值越高越深紫色越浅。但我在这里得到的似乎完全不同。enter image description here。似乎越来越少的负价值越来越暗,更多的积极价值被忽视。我是不是做错了什么?在

x = ["A","B","C","D"]
y = [-0.086552691,0.498737914,-0.090153413,-0.075941404]

sns.axes_style('white')
sns.set_style('white')
pal=sns.cubehelix_palette(5)
ax = sns.barplot(x, y,palette=pal)

for n, (label, _y) in enumerate(zip(x, y)):
    ax.annotate(
        s='{:.3f}'.format(_y),
        xy=(n, _y),
        ha='center',va='center',
        xytext=(0,10*(1 if _y > 0 else -1)),
        textcoords='offset points',
        size = 8,
        weight='bold'
    )
    ax.annotate(
        s=label,
        xy=(n, 0),
        ha='left',va='center',
        xytext=(0,50*(-1 if _y > 0 else 1)),
        textcoords='offset points',
        rotation=90,
        size = 10,
        weight='bold'
    )  
# axes formatting
#ax.set_yticks([])
ax.set_xticks([])
sns.despine(ax=ax, bottom=True, left=True)

已编辑 根据@ImportanceOfBeingErnest的建议,我也尝试了以下代码。然而,负方向强度是错误的。传说也令人不安。 enter image description here

^{pr2}$

Tags: 代码styleaxlabelcenter价值whiteset
1条回答
网友
1楼 · 发布于 2024-03-29 07:37:52

documentation表示您的palette参数将您的颜色映射到您的hue参数的不同级别,而您尚未提供这些参数。在

所以我认为你需要在你的barplot中设置hue参数,这样你的颜色就可以专门映射到你的y值。在

除了将ax = sns.barplot(x, y,palette=pal)替换为以下内容外,其他所有内容都保持不变:

ax = sns.barplot(x, y, hue=y, palette=pal, dodge=False)

# Remove the legend
ax.legend_.remove()

您得到了这个图,其中y越高,颜色越暗:

enter image description here

相关问题 更多 >