创建具有“中频"的发散调色板而不是“中点" [英] Creating a diverging color palette with a "midrange" instead of a "midpoint"

查看:44
本文介绍了创建具有“中频"的发散调色板而不是“中点"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using python seaborn package to generate a diverging color palette (seaborn.diverging_palette).

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

I can choose my two extremity colors, and define if the center is light-> white or dark->black (centerparameter). But what I would like is to extend this center part color (white in my case) to a given range of values.

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

For example, my values are from 0 to 20. So, my midpoint is 10. Hence, only 10 is in white, and then it becomes more green/more blue when going to 0/20. I would like to keep the color white from 7 to 13 (3 before/after the midpont), and then start to move to green/blue.

我找到了 sep 参数,它扩展或减少了这个中心的白色部分.但是我找不到关于它的值的含义的任何解释,例如,为了找到 sep 的哪个值将对应于中点的每一侧 3.

I found the sep parameter, which extends or reduces this center white part. But I can't find any explanation on what its value means, in order to find which value of sepwould correspond to 3 each side of the midpoint for example.

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

Does anybody know the relationship between sep and the value scale ? Or if another parameter could do the expected behaviour ?

推荐答案

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

It seems the sep parameter can take any integer between 1 and 254. The fraction of the colourmap that will be covered by the midpoint colour will be equal to sep/256.

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

Perhaps an easy way to visualise this is to use the seaborn.palplot, with n=256 to split the palette up into 256 colours.

这是一个带有 sep = 1 的调色板:

Here is a palette with 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(即调色板的四分之一是中点颜色)

Here is sep = 64 (i.e. one quarter of the palette is the midpoint colour)

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

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

Here is sep = 128 (i.e. one half is the midpoint colour)

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

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

And here is sep = 254 (i.e. all but the colours on the very edge of the palette are the midpoint colour)

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

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

So, for your case where you have a range of 0 - 20, but a midpoint range of 7 - 13, you would want the fraction of the palette to be the midpoint to be 6/20. To convert that to sep, we need to multiply by 256, so we get sep = 256 * 6 / 20 = 76.8. However, sep must be an integer, so lets use 77.

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

Here is a script to make a diverging palette, and plot a colorbar to show that using sep = 77 leaves the correct midpoint colour between 7 and 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()

这篇关于创建具有“中频"的发散调色板而不是“中点"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆