我正在使用以下代码使用SNS绘制以下类型的条形图。我使用了cubehelix_palette
,因为我想要根据值的条形颜色强度。我希望值越高越深紫色越浅。但我在这里得到的似乎完全不同。。似乎越来越少的负价值越来越暗,更多的积极价值被忽视。我是不是做错了什么?在
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的建议,我也尝试了以下代码。然而,负方向强度是错误的。传说也令人不安。
^{pr2}$
documentation表示您的
palette
参数将您的颜色映射到您的hue
参数的不同级别,而您尚未提供这些参数。在所以我认为你需要在你的
barplot
中设置hue
参数,这样你的颜色就可以专门映射到你的y
值。在除了将
ax = sns.barplot(x, y,palette=pal)
替换为以下内容外,其他所有内容都保持不变:您得到了这个图,其中
y
越高,颜色越暗:相关问题 更多 >
编程相关推荐