Skip to content

Open in Colab

Colourmaps

A bivariate colourmap is a function: it takes two arrays of values and returns an array of RGB colours. The colours are blended in the OKLab space, so that the steps between them look even.

import matplotlib.pyplot as plt

from bivario import (
    AccentsBivariateColourmap,
    CornersBivariateColourmap,
    MplCmapBivariateColourmap,
    NamedBivariateColourmap,
    get_bivariate_cmap,
    plot_bivariate_legend,
)
from bivario.example_data import nyc_bike_trips

Named palettes

get_bivariate_cmap without arguments returns the default one, with a name it returns one of the available palettes. In a notebook, a colourmap shows itself: the first variable grows upwards, the second one to the right.

get_bivariate_cmap()
NamedBivariateColourmap (rosewood_pine)
NamedBivariateColourmap (rosewood_pine)
NamedBivariateColourmap("sunrise_harbor")
NamedBivariateColourmap (sunrise_harbor)
NamedBivariateColourmap (sunrise_harbor)

invert_accents swaps the colours of the two variables, dark_mode the colours of low and high values, so that low ones fade into a dark background:

NamedBivariateColourmap("sunrise_harbor", invert_accents=True)
NamedBivariateColourmap (sunrise_harbor)
NamedBivariateColourmap (sunrise_harbor)
NamedBivariateColourmap("sunrise_harbor", dark_mode=True)
NamedBivariateColourmap (sunrise_harbor)
NamedBivariateColourmap (sunrise_harbor)

Two accents

The quickest way to a palette of your own: one colour for each of the variables. Low values are white and high ones are dark grey, unless you change them.

AccentsBivariateColourmap(accent_a="crimson", accent_b="dodgerblue")
AccentsBivariateColourmap (Accent A: #dc143c, Accent B: #1e90ff, Light: #ffffff, Dark: #262626)
AccentsBivariateColourmap (Accent A: #dc143c, Accent B: #1e90ff, Light: #ffffff, Dark: #262626)
AccentsBivariateColourmap("crimson", "dodgerblue", light="lemonchiffon", dark="midnightblue")
AccentsBivariateColourmap (Accent A: #dc143c, Accent B: #1e90ff, Light: #fffacd, Dark: #191970)
AccentsBivariateColourmap (Accent A: #dc143c, Accent B: #1e90ff, Light: #fffacd, Dark: #191970)

Four corners

All the four colours at once:

CornersBivariateColourmap(accent_a="#ff7f11", accent_b="#2ec4b6", low="#fdfcdc", high="#3d348b")
CornersBivariateColourmap (Accent A: #ff7f11, Accent B: #2ec4b6, Low: #fdfcdc, High: #3d348b)
CornersBivariateColourmap (Accent A: #ff7f11, Accent B: #2ec4b6, Low: #fdfcdc, High: #3d348b)

Matplotlib's colourmaps

Two sequential colourmaps, one for each of the variables:

MplCmapBivariateColourmap("Oranges", "Blues")
MplCmapBivariateColourmap (Oranges, Blues)
MplCmapBivariateColourmap (Oranges, Blues)

From values to colours

Call a colourmap with two arrays, lists or series. The values are normalized first, unless normalize=False is passed:

cmap = NamedBivariateColourmap("bubblegum")
cmap([0, 5, 10], [0, 50, 100])
array([[ 0.99000373,  0.96002426,  0.86000738],
       [ 0.62001418,  0.56508251,  0.65503522],
       [ 0.14000957,  0.16000209,  0.34000105]])
cmap([0.1, 0.2], [0.1, 0.9], normalize=False)
array([[ 0.92894978,  0.88301761,  0.82889479],
       [ 0.79005632,  0.3625995 ,  0.5518611 ]])

Any plot that takes colours can use them, not only maps:

trips = nyc_bike_trips()
trips = trips[(trips["morning_starts"] > 0) & (trips["morning_ends"] > 0)]
starts, ends = trips["morning_starts"], trips["morning_ends"]

fig, ax = plt.subplots(figsize=(6, 6), dpi=150)
ax.scatter(ends, starts, c=cmap(starts.rank(), ends.rank()), s=12)
ax.set(xscale="log", yscale="log", xlabel="Morning ends", ylabel="Morning starts")
plt.show()

png

Legend

plot_bivariate_legend draws the colourmap with the ranges of the values on its axes. It takes a name, an object, or nothing for the default palette:

fig, axs = plt.subplots(1, 3, figsize=(15, 5), dpi=100, layout="constrained")
plot_bivariate_legend(starts, ends, ax=axs[0], label_a="Starts", label_b="Ends")
plot_bivariate_legend(starts, ends, ax=axs[1], cmap=cmap, grid_size=5)
plot_bivariate_legend(
    starts, ends, ax=axs[2], cmap=MplCmapBivariateColourmap("Oranges", "Blues"), grid_size=(4, 2)
)
plt.show()

png