62

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.

Serenity
  • 35,289
  • 20
  • 120
  • 115
Dremet
  • 1,008
  • 1
  • 9
  • 23

5 Answers5

59

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

Serenity
  • 35,289
  • 20
  • 120
  • 115
  • 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 '16 at 09: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 '16 at 17:12
  • 3
    Based on this answer @Corrumpo you code use `cmap = ListedColormap(sns.color_palette().as_hex())` – RafaelLopes Dec 02 '16 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 '17 at 09: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)`. – ImportanceOfBeingErnest Feb 03 '18 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

Ramon Crehuet
  • 3,679
  • 1
  • 22
  • 37
29

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())
Generic Wevers
  • 570
  • 6
  • 10
  • 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? – Kristada673 Apr 17 '19 at 04: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))   
LauraD
  • 121
  • 1
  • 4
0

Pandas plots (which are matplotlib plots) need ListedColormap to define a list of colors. They can also use seaborn colour palettes directly if they have been created with the as_cmap=True argument.

Create a sample dataset

import pandas
import seaborn
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
df = pandas.DataFrame({'x':range(0,30), 'y':range(10,40)})
df.set_index('x', inplace=True)
df["z"] = 39
df["a"] = 10

Use a simple list of colours to plot 3 data frame columns (this doesn't require seaborn)

df.plot(colormap=ListedColormap(["red","green","orange"]), figsize=(3,3))
plt.show()

plot illustrating a change in palette

Use a seaborn palette defined with the as_cmap=True argument:

palette = seaborn.color_palette("rocket_r", as_cmap=True)
df.plot(colormap=palette, figsize=(3,3))
plt.show()

enter image description here

Thanks to Serenity, Generic Wevers and Ramon Crehuet for their answers.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110