在seaborn bar p中更改色阶

2024-07-08 14:42:15 发布

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

我想使用seaborn条形图作为我的数据,根据Y轴上的值使用色阶。例如,在此图像中,颜色根据调色板从左到右变化:

enter image description here

但我真正想要的是同样的颜色方案,但在“垂直”而不是“水平”。这可能吗?我已经搜索并尝试将hue参数设置为Y轴,但它似乎不起作用,我如何才能做到?

提前谢谢。


Tags: 数据图像颜色水平方案seabornhue条形图
1条回答
网友
1楼 · 发布于 2024-07-08 14:42:15

这里有一个解决方案:

import numpy as np, matplotlib.pyplot as plt, seaborn as sns
sns.set(style="whitegrid", color_codes=True)

titanic = sns.load_dataset("titanic")
data = titanic.groupby("deck").size()   # data underlying bar plot in question

pal = sns.color_palette("Greens_d", len(data))
rank = data.argsort().argsort()   # http://stackoverflow.com/a/6266510/1628638
sns.barplot(x=data.index, y=data, palette=np.array(pal[::-1])[rank])

plt.show()

这里是输出: bar plot

注意:代码当前为相同高度的条指定不同(相邻)颜色。(在示例图中没有问题)虽然对相同的高度条使用相同的颜色会更好,但是生成的代码可能会使基本思想不那么清晰。

相关问题 更多 >

    热门问题