Python 2.7 创建一个带有“";“中档”;而不是",;中点;

Python 2.7 创建一个带有“";“中档”;而不是",;中点;,python-2.7,seaborn,color-palette,Python 2.7,Seaborn,Color Palette,我正在使用python seaborn包生成一个发散调色板(seaborn.diverging_palette) 我可以选择我的两种极端颜色,并定义中心是亮->白还是暗->黑(centerparameter)。但是我想把这个中心部分的颜色(在我的例子中是白色)扩展到一个给定的值范围 例如,我的值从0到20。所以,我的中点是10。因此,只有10是白色的,当转到0/20时,它会变得更绿/更蓝。我想保持白色从7到13(中间桥前/后3),然后开始移动到绿色/蓝色 我找到了sep参数,它扩展或减少了中间的

我正在使用python seaborn包生成一个发散调色板(seaborn.diverging_palette)

我可以选择我的两种极端颜色,并定义中心是亮->白还是暗->黑(
center
parameter)。但是我想把这个中心部分的颜色(在我的例子中是白色)扩展到一个给定的值范围

例如,我的值从0到20。所以,我的中点是10。因此,只有10是白色的,当转到0/20时,它会变得更绿/更蓝。我想保持白色从7到13(中间桥前/后3),然后开始移动到绿色/蓝色

我找到了
sep
参数,它扩展或减少了中间的白色部分。但我找不到任何关于其值含义的解释,例如,为了找到
sep
的哪个值对应于中点每边3

有人知道sep和价值量表之间的关系吗?
或者另一个参数是否可以执行预期的行为?

似乎
sep
参数可以采用
1
254
之间的任何整数。中点颜色覆盖的颜色图分数将等于
sep/256

也许一个简单的可视化方法是使用
seaborn.palplot
,使用
n=256
将调色板分成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
(即调色板的四分之一是中点颜色)

这是
sep=128
(即一半是中点颜色)

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

您的特定调色板 因此,对于范围为
0-20
但中点范围为
7-13
的情况,您希望调色板的分数作为中点为
6/20
。要将其转换为
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
cmap = plt.gcf().colorbar(hmap)
cmap.set_ticks(range(21))

plt.show()

sns.帕尔普洛特的电话丢失了一个右键parenthesis@Tyler谢谢,修好了
sns.palplot(sns.diverging_palette(0, 255, sep=128, n=256))
sns.palplot(sns.diverging_palette(0, 255, sep=254, n=256))
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
cmap = plt.gcf().colorbar(hmap)
cmap.set_ticks(range(21))

plt.show()