我正在尝试使用自定义的heatmap()创建一个相关矩阵的海运diverging_palette()。我想让100%红色在极端,100%白色在中间。
我使用了下面的代码,但我得到了粉红在极端,浅灰在中心(见屏幕截图)。我会说hsl(0, 100%, 50%)应该给红色?我遗漏了什么?
corr_matrix = ames_train_cleaned.select_dtypes(include=[np.number]).corr()
fig, ax = plt.subplots(figsize=(15,15))
my_palette = sns.diverging_palette(h_neg=0, h_pos=0, s=100, l=50, sep=100, as_cmap=True)
_ = sns.heatmap(data=corr_matrix,
ax=ax,
mask=np.triu(corr_matrix, k=1),
cmap=my_palette,
center=0)

发布于 2022-06-16 21:41:34
回答我自己的问题。我在StackOverflow上发现了这个问题,它为我指明了正确的方向。
Seaborn diverging_palette with more than 2 color tones
将代码片段中my_palette的定义替换为:
my_palette = LinearSegmentedColormap.from_list(name='dummy', colors=['blue','white','red'])
这是完美的,但我仍然不知道为什么红色在我最初的解决方案中不是红色.
https://stackoverflow.com/questions/72651095
复制相似问题