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.
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:
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.
Four corners¶
All the four colours at once:
Matplotlib's colourmaps¶
Two sequential colourmaps, one for each of the variables:
From values to colours¶
Call a colourmap with two arrays, lists or series. The values are normalized first, unless normalize=False is passed:
array([[ 0.99000373, 0.96002426, 0.86000738],
[ 0.62001418, 0.56508251, 0.65503522],
[ 0.14000957, 0.16000209, 0.34000105]])
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()

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()
