2

I'm reading and testing the seaborn palette tutorial. I would be interested in a dark diverging palette based on a predefined matplotlib colormap. Is that possible? (Does that even make sense in all cases?)

As I actually only want 3 colours, "black" being the middle one, I can extract the first and last of a palette as follows:

import seaborn as sns

left = sns.color_palette("BrBG")[0]
right = sns.color_palette("BrBG")[-1]
sns.palplot([left, "black", right])

enter image description here

I see that if I increase the number of elements in the source palette, I can obtain darker colours as left and right:

left = sns.color_palette("BrBG", 12)[0]
right = sns.color_palette("BrBG", 12)[-1]
sns.palplot([left, "black", right])

enter image description here

I'm wondering how far dark the left and right colour can go if I increase the number of elements. I suppose they don't end up being black, otherwise the two extremes would become impossible to distinguish.

Is it possible to extract these "extreme" colours directly from the corresponding matplotlib colormap?

I can also go towards lighter colours asking for less elements in the palette than the default (which is 6). Asking for 2 elements results in something too light for my taste:

left = sns.color_palette("BrBG", 2)[0]
right = sns.color_palette("BrBG", 2)[-1]
sns.palplot([left, "black", right])

enter image description here

Anyway, this approach is not very "clean", and the extremes of a colormap are not necessarily a good choice. Besides, a generalization may be useful: How to create a dark diverging palette with more than 3 colours, based on a predefined colormap?

The above mentioned tutorial says that the function to create custom diverging palettes results in something well balanced and gives the following example:

sns.palplot(sns.diverging_palette(255, 133, l=60, n=7, center="dark")

I guess my question amounts to the following:

How do I find the angle values corresponding to the hues of the two sides of a predefined matplotlib colormap?

bli
  • 7,549
  • 7
  • 48
  • 94
  • The following may be useful for my first question: http://stackoverflow.com/a/33597599/1878788 – bli Nov 04 '16 at 14:22

1 Answers1

3

Getting inspired by https://stackoverflow.com/a/33597599/1878788, I tried the following:

import matplotlib as mpl
cmap = mpl.cm.get_cmap("BrBG")
left = cmap(0)
right = cmap(cmap.N)
sns.palplot([left, "black", right])

enter image description here

So I have now a partial answer. And it looks too dark.

Edit: Getting the angle of a colour in the husl system

The answer lies in the husl module: https://pypi.python.org/pypi/husl/

import husl
# Extract rgb coordinates
left_rgb = cmap(0)[:-1]
right_rgb = cmap(cmap.N)[:-1]
# Convert to husl and take the hue (first element)
left_hue = husl.rgb_to_husl(*left_rgb)[0]
right_hue = husl.rgb_to_husl(*right_rgb)[0]
# Create the palette and plot it
sns.palplot(sns.diverging_palette(left_hue, right_hue, n=3, center="dark"))

enter image description here

Community
  • 1
  • 1
bli
  • 7,549
  • 7
  • 48
  • 94