Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a colormap as a palette in Seaborn

This is probably a misunderstanding how colormaps are different from palettes, but I'd like to use a colormap that is not available in seaborn for coloring my binned dataset. I tried using palettable and now cmocean in particular directly but will get a TypeError;

'LinearSegmentedColormap' object is not iterable

Using any of the palettes that are available in Seaborn will work just fine, but I need a palette that doesn't go to white as this adds a weird 'banding' to the plot.

I have a dataframe with 3 columns with numerical data, dimensions and added a bin column for the colors usage in the plot.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import cmocean

cmap=cmocean.cm.balance
cpal=sns.color_palette(cmap,n_colors=64,desat=0.2)

plt.style.use("seaborn-dark")
ax = sns.stripplot(x='Data', y='Dimension', data=dfBalance, jitter=0.15, edgecolor='none', alpha=0.4, size=4, hue='bin', palette=cpal)
sns.despine()
ax.legend_.remove()
plt.show()
like image 942
Chrisvdberge Avatar asked Oct 10 '18 08:10

Chrisvdberge


People also ask

What is Seaborn palette?

The default color palette in seaborn is a qualitative palette with ten distinct hues: sns. color_palette() These colors have the same ordering as the default matplotlib color palette, "tab10" , but they are a bit less intense.

How do you set up a color palette?

Pick one color and find its complementary color (the one right across from it on the color wheel). Then find the colors on either side of the complementary color. Those two colors and your original color make up a split complementary color scheme.


1 Answers

Seaborn does not take a Colormap instance as input for .color_palette. It takes

name of matplotlib cmap, [...], or a list of colors in any format matplotlib accepts

Since cmocean registers its colormaps with matplotlib with a "cmo." prefix, you would do

import seaborn as sns
import cmocean

cpal = sns.color_palette("cmo.balance", n_colors=64, desat=0.2)

In case you have a custom colormap created yourself or from any other package, you might register it yourself.

import seaborn as sns
import matplotlib.cm
import matplotlib.colors

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["brown", "pink", "limegreen"])
matplotlib.cm.register_cmap("mycolormap", cmap)
cpal = sns.color_palette("mycolormap", n_colors=64, desat=0.2)
like image 54
ImportanceOfBeingErnest Avatar answered Oct 04 '22 23:10

ImportanceOfBeingErnest