Extending Palettes with Glasbey

The glasbey library allows you to extend colour palettes with extra colours for extra categories using techniques from the paper Colour Displays for Categorical Images by Glasbey, Heijden, Toh and Gray. For a demonstration of this in action we need to import the glasbey library, and to visualize the palettes well use seaborn for convenience.

[1]:
import glasbey
import seaborn as sns

sns.set()

Have you ever been working with a plot and found that you have just a few more categories than the colour palette supports, resulting in colour cycling and two categories sharing a colour? Have you ever wished you could just add a few extra colours to an existing palette without having to painstakingly pick out suitably distinct colours yourself? Help is at hand!

Extending an existing palette is as easy as calling extend_palette. You will need to provide a palette to be extended. For ease of use you can specify any of matplotlib’s palettes (which includes many of the ColorBrewer palettes among others. Here we will extend the default matplotlib palette, tab10, to have fifteen colours.

[2]:
glasbey.extend_palette("tab10", palette_size=15)
[2]:
['#1f77b4',
 '#ff7f0e',
 '#2ca02c',
 '#d62728',
 '#9467bd',
 '#8c564b',
 '#e377c2',
 '#7f7f7f',
 '#bcbd22',
 '#17becf',
 '#a6b2ff',
 '#d7aa9e',
 '#8e7500',
 '#b20096',
 '#8eba96']

Staring at hex-codes is not that helpful, so let’s use seaborn’s palplot to plot the palette and see what we ended up with…

[3]:
sns.palplot(glasbey.extend_palette("tab10", palette_size=15))
_images/extending_palettes_5_0.png

The result is the standard tab10, or Category10, palette but with five new colours added, and they are, indeed, fairly distinct from the existing palette, and somewhat in keeping with the style of colours used so far.

Just as with create_palette we can specify specific bounds on the lightness, chroma, and hue of generated colours. This can be particularly useful if you want to tweak the generated colours to be more in line with the esiting palette. For example here is the Set1 ColorBrewer palette with four new colours where we have specifically constrained the lightness so that the new colours are more in keeping with the original palette.

[4]:
sns.palplot(glasbey.extend_palette("Set1", palette_size=12, lightness_bounds=(30, 60)))
_images/extending_palettes_7_0.png

Note that if you don’t specify bounds, as long as the provided palette has enough colours (at least four) then the bounds will be inferred from the provided palette (based on the min and max of lightness, chroma and hue in that palette). The inferred bounds are usually sufficient for a reasonable match, but you can specify more specific bounds yourself if needed.

Extending an existing palette is useful, but there is more that can be done. It is not uncommon to want to have a categorical colour palette based around a specific colour (perhaps your company or institution colour). We can extend a palette based on a single colour just as easily …

[5]:
sns.palplot(glasbey.extend_palette("#2a3e63", palette_size=12, lightness_bounds=(10, 40)))
_images/extending_palettes_9_0.png

With only a single colour it is not reasonable to attempt to infer bounds, so here it can be useful to play with lightness, chroma and hue bounds to get the aesthetic for the palette that you want.

Of course perhaps you have a small set of colours and just want a small categorical palette built thematically around it. In this case you just need to provide a list of colours.

[6]:
sns.palplot(glasbey.extend_palette(["#2a3e63", "#7088b8", "#fcaf3e", "#b87088"], palette_size=8))
_images/extending_palettes_11_0.png

And of course if you want to make a very large palette, extending on from an existing palette, you can simply ask for a palette as large as you need.

[7]:
sns.palplot(glasbey.extend_palette("tab10", palette_size=32))
_images/extending_palettes_13_0.png