使用 "midrange" 而不是 "midpoint" 创建发散调色板

Creating a diverging color palette with a "midrange" instead of a "midpoint"

我正在使用 python seaborn 包来生成不同的调色板 (seaborn.diverging_palette)。

我可以选择我的两个肢体颜色,并定义中心是浅色->白色还是深色->黑色(center参数)。但是我想要的是将这个中心部分的颜色(在我的例子中是白色)扩展到给定的值范围。

例如,我的值是从 0 到 20。所以,我的中点是 10。因此,只有 10 是白色的,然后到 0/20 时它变得更 green/more 蓝色。我想将白色从 7 保持到 13(3 before/after 中桥),然后开始移动到 green/blue.

我找到了 sep 参数,它扩展或缩小了这个中心白色部分。但是我找不到关于它的值意味着什么的任何解释,例如为了找到 sep 的哪个值对应于中点每一侧的 3。

有人知道 sep 和值标度之间的关系吗? 或者如果另一个参数可以执行预期的行为?

sep 参数似乎可以取 1254 之间的任何整数。将被中点颜色覆盖的颜色图部分将等于 sep/256.

也许一个简单的可视化方法是使用 seaborn.palplot,将调色板分成 256 种颜色。

这是一个调色板 sep = 1:

sns.palplot(sns.diverging_palette(0, 255, sep=1, n=256))

这是一个调色板 sep = 8

sns.palplot(sns.diverging_palette(0, 255, sep=8, n=256))

这里是sep = 64(即调色板的四分之一是中点颜色)

sns.palplot(sns.diverging_palette(0, 255, sep=64, n=256))

这里是sep = 128(即二分之一是中点颜色)

sns.palplot(sns.diverging_palette(0, 255, sep=128, n=256))

这里是 sep = 254(即除了调色板最边缘的所有颜色都是中点颜色)

sns.palplot(sns.diverging_palette(0, 255, sep=254, n=256))

您的特定调色板

因此,对于您的范围为 0 - 20,但中点范围为 7 - 13 的情况,您希望调色板的分数为中点 [=29] =].要将其转换为 sep,我们需要乘以 256,所以我们得到 sep = 256 * 6 / 20 = 76.8。但是,sep 必须是整数,所以我们使用 77.

这是一个制作发散调色板的脚本,并绘制了一个颜色条以显示使用 sep = 77 会在 7 和 13 之间留下正确的中点颜色:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# Create your palette
cmap = sns.diverging_palette(0, 255, sep=77, as_cmap=True)

# Some data with a range of 0 to 20
x = np.linspace(0, 20, 20).reshape(4, 5)

# Plot a heatmap (I turned off the cbar here,
# so I can create it later with ticks spaced every integer)
ax = sns.heatmap(x, cmap=cmap, vmin=0, vmax=20, cbar=False)

# Grab the heatmap from the axes
hmap = ax.collections[0]

# make a colorbar with ticks spaced every integer
cbar = plt.gcf().colorbar(hmap)
cbar.set_ticks(range(21))

plt.show()