Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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.

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.

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

like image 292
pip Avatar asked Mar 08 '16 13:03

pip


1 Answers

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.

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

Here is a palette with sep = 1:

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

enter image description here

And here is a palette with sep = 8

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

enter image description here

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))

enter image description here

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

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

enter image description here

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))

enter image description here

Your specific palette

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.

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
cbar = plt.gcf().colorbar(hmap)
cbar.set_ticks(range(21))

plt.show()

enter image description here

like image 103
tmdavison Avatar answered Oct 05 '22 12:10

tmdavison