Reputation: 52253
How to obtain the cmap version of the palette that comes from sns.color_palette()
function? For example, I can ask for the color brewer palette like this:
sns.color_palette('Blues')
but
sns.color_palette('Blues', as_cmap=True)
results in "TypeError: color_palette() got an unexpected keyword argument 'as_cmap'".
I could of course use sns.choose_colorbrewer_palette()
, which accepts as_cmap
argument, but it only works in interactive mode (I can't ask it for 'Blues'
in the python script).
Upvotes: 4
Views: 6496
Reputation: 52253
Just want to add something that was confusing for me.
For palettes that support it, if you set as_cmap=True
, seaborn
creates a color palette with 256 colors and then asks mpl.ListedColorMap
to make an mpl.ColorMap
(which will simply interpolate between the neighboring colors, treating them as if they split [0, 1] interval into equal parts). It makes sense that seaborn
doesn't use the n_colors
argument in this case because a mpl.ColorMap
represents a function from [0, 1] into colors, and the more colors are used to create this with mpl.ListedColorMap
the more precise the function is. (Presumably no need to go above 256 colors due for most applications?)
So converting a seaborn color palette to mpl.ColorMap
can be done by generating 256 colors of the palette (assuming the palette is generated algorithmically of course, so it produces that many distinct colors) and then calling mpl.ListedColorMap
. Of course, as @mwaskom pointed out one can simply use the mpl.ColorMap
object from mpl.cm
namespace.
Upvotes: 2
Reputation: 49002
If you want to use the colormap in a matplotlib/seaborn function, simply pass the name (e.g., plt.pcolormesh(..., cmap="Blues")
) and matplotlib will know what to do.
If you want the colormap object for some reason, it lives in the matplotlib.cm
namespace.
Upvotes: 3