Joanna
Joanna

Reputation: 81

How to make a color map with many unique colors in seaborn

I want to make a colormap with many (in the order of hundreds) unique colors.

This code:

custom_palette = sns.color_palette("Paired", 12)
sns.palplot(custom_palette)

returns a palplot with 12 unique colors.
But this code:

custom_palette = sns.color_palette("Paired", 24)
sns.palplot(custom_palette)

returns a palplot with 12 unique colors, seemingly repeated twice.

How do I obtain 24 (or more) unique colors?

Upvotes: 7

Views: 30290

Answers (3)

rocksNwaves
rocksNwaves

Reputation: 6164

There is definitely a way to get what you want! Fortunately, Seaborn has the ability to read colorcet palettes which have some nice options for getting a wider range of qualitative colors:

from sklearn.datasets import make_blobs
import colorcet as cc
import matplotlib.pyplot as plt
import seaborn as sns


blobs, labels = make_blobs(n_samples=1000, centers=25, center_box=(-100, 100))
palette = sns.color_palette(cc.glasbey, n_colors=25)

sns.scatterplot(x=blobs[:,0], y=blobs[:, 1], hue=labels, data=blobs, palette=palette)
plt.legend(ncol=5, bbox_to_anchor=(1, 1))
plt.show()

Results in the following graph with 25 distinct colors:

enter image description here

Hope this helps!

Upvotes: 22

mwaskom
mwaskom

Reputation: 49032

I suppose you have a few options.

With a circular color system you can generate an arbitrarily large number* of distinct hues. These will be "ordered", but you could generate a list and permute it, and even combine multiple lists generated by varying the lightness/saturation values before doing so.

The colorcet package includes the Glasbey colormaps, which have a large number of distinct hues with some variation in lightness/saturation and no particular ordering. You can use reference by name colormaps from any package that registers its colormaps with matplotlib; or you could generate a list and pass that.

* This is not really true, for a boring reason and an interesting reason. The boring reason is that most monitors are 8 bit so you only really get 256 distinct values for each RGB gun. The more interesting (and problematic) reason is that human perception cannot distinguish colors just because they have different 8 bit RGB values. So you can make a plot with a huge number of colors, but it won't really appear that way to a viewer. Exactly how big of a problem this is depends on your application, but it's something very important to keep in mind.

Upvotes: 2

djangoliv
djangoliv

Reputation: 1788

The "Paired" color palette only has 12 colors, so you cannot have more than 12 different colors.

Chose a Sequential color palette to have more than 12 colors.

"rocket", "mako" or "viridis" for example:

custom_palette = sns.color_palette("viridis", 24)
sns.palplot(custom_palette)

https://seaborn.pydata.org/tutorial/color_palettes.html

Upvotes: 4

Related Questions