58

Seaborn offers a function called color_palette, which allows you to easily create new color_palettes for plots.

colors = ["#67E568","#257F27","#08420D","#FFF000","#FFB62B","#E56124","#E53E30","#7F2353","#F911FF","#9F8CA6"]

color_palette = sns.color_palette(colors)

I want to transform color_palette to a cmap, which I can use in matplotlib, but I don't see how I can do this.

Sadly just functions like "cubehelix_palette","light_palette",… have an "as_cmap" paramater. "color_palette" doesn't, unfortunately.

4 Answers 4

56

You have to convert a list of colors from seaborn palette to color map of matplolib (thx to @RafaelLopes for proposed changes):

import seaborn as sns
import matplotlib.pylab as plt
import numpy as np
from matplotlib.colors import ListedColormap

# construct cmap
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
my_cmap = ListedColormap(sns.color_palette(flatui).as_hex())

N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

enter image description here

5
  • 2
    Correct me, if I am wrong, but this doesn't look like a transformation of a seaborn color_palette to a matplotlib cmap, this looks like an alternative solution on how to make a colormap with matplotlib. If I remove sns.set_palette(flatui) from your code nothing changes. Nevertheless thanks for showing this matplotlib functionality.
    – Dremet
    Jun 19, 2016 at 9:51
  • 4
    This is the correct answer in terms of functionality, but please don't use discrete sets of colors to map continuously varying data, it is very misleading.
    – mwaskom
    Jun 19, 2016 at 17:12
  • 3
    Based on this answer @Corrumpo you code use cmap = ListedColormap(sns.color_palette().as_hex()) Dec 2, 2016 at 12:22
  • @RafaelLopes thanks for your comment! This is very helpful and allows practically using every seaborn palette directly in matplotlib! :)
    – chAlexey
    Apr 11, 2017 at 9:28
  • 1
    Note that this answer creates a matplotlib colormap from a list of colors. There is no need to use seaborn here at all, the above code could be done completely without the use of seaborn. It seems that the original version of this answer actually did exactly that, using my_cmap = ListedColormap(flatui). Feb 3, 2018 at 21:09
41

Most seaborn methods to generate color palettes have an optional argument as_cmap which by default is False. You can use to directly get a Matplotlib colormap:

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

# construct cmap
my_cmap = sns.light_palette("Navy", as_cmap=True)

N = 500
data1 = np.random.randn(N)
data2 = np.random.randn(N)
colors = np.linspace(0,1,N)
plt.scatter(data1, data2, c=colors, cmap=my_cmap)
plt.colorbar()
plt.show()

enter image description here

0
28

The first answer is somehow correct but way too long with a lot of unnecessary information. The correct and short answer is:

To convert any sns.color_palette() to a matplotlib compatible cmap you need two lines of code

from matplotlib.colors import ListedColormap
cmap = ListedColormap(sns.color_palette())
1
  • 3
    And for the opposite? Is there a similar code to convert a matplotlib colormap like [0.0, 'rgb(30, 136, 229)'], [0.01, 'rgb(30, 135, 228)'], ... into a seaborn compatible color palette? Apr 17, 2019 at 4:53
12

Just an additional tip - if one wants a continuous colorbar/colormap, adding 256 as the number of colors required from Seaborn colorscheme helps a lot.

cmap = ListedColormap(sns.color_palette("Spectral",256))   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.