mpl-histcolorbar: a histogram + colorbar for matplotlib

Installation

mpl-histcolorbar is available as mpl_histcolorbar on PyPI:

pip install mpl_histcolorbar

Dependencies

Basic usage

The class HistColorbar is designed to be a drop-in replacement for matplotlib’s Colorbar:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_histcolorbar import HistColorbar

# Generate some data
rng = np.random.default_rng(seed=42)
data = rng.standard_normal(size=(25, 100))

# Plot an image
fig, ax = plt.subplots()
im = ax.imshow(data)

# Create an axis for the HistColorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.02)

# Add the HistColorbar
hcb = HistColorbar(cax, im)
Example of using a HistColorbar object

A Colorbar in matplotlib is often created by calling the colorbar method of a Figure. mpl-histcolorbar provides an equivalent function histcolorbar, which takes a Figure object as its first argument:

import numpy as np
import matplotlib.pyplot as plt
from mpl_histcolorbar import histcolorbar

# Generate some data
rng = np.random.default_rng(seed=42)
data = rng.standard_normal(size=(25, 100))

# Plot an image
fig, ax = plt.subplots()
im = ax.imshow(data)

# Add the HistColorbar
hcb = histcolorbar(fig, im, location="bottom")
Example of calling histcolorbar