Skip to content

Multivar colorbar - #32607

Draft
trygvrad wants to merge 3 commits into
matplotlib:mainfrom
trygvrad:multivar-colorbar
Draft

trygvrad wants to merge 3 commits into
matplotlib:mainfrom
trygvrad:multivar-colorbar

Conversation

@trygvrad

@trygvrad trygvrad commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

This PR has a long history. It is part of Bivariate and Multivariate Colormapping and contains changes that were previously part of #31214 which is being separated into multiple parts with the use of AI. This is part 3 of 4 and Depends on #32605 and #32606. Only the last commit ("Add multivariate colorbars") is new in this PR.

PR summary

  • Adds Figure.colorbar_multivar / SubFigure.colorbar_multivar and pyplot.colorbar_multivar.
    MultivarColorbar (in matplotlib.colorbar_multivariate) is a Sequence of ordinary Colorbar objects, one per variable, as jklymak suggested in Multivar colorbar #31214.
  • make_multivar_axes takes the space for the whole block through the existing colorbar.make_axes (as for a single colorbar), then splits it into one Axes per variable.
  • n_major arranges the colorbars in several rows/columns; major_pad / minor_pad set the spacing.
    Colorbars can also be drawn into user-supplied Axes with caxes=.
  • Figure.colorbar raises a clear error for multivariate colormaps, pointing to colorbar_multivar.
  • Release note with a plot in doc/release/next_whats_new/colorbar_multivar.rst.

NOTE:

  • Constrained layout is not supported in this PR: it raises a NotImplementedError unless caxes is given. Support follows in part 4, kept separate so the layout-engine changes can be reviewed on their own.

There is no example in the docs yet, that will come in a separate PR.

Here is an example:

im_A = np.arange(200)[np.newaxis, :]*np.ones((200, 200))
im_B = np.arange(200)[:, np.newaxis]*np.ones((200, 200))
im_C = 0.9*im_A + 0.9*im_B

im_A = np.sin(im_A**0.5)**2
im_B = np.sin(im_B**0.5)**2
im_C = np.sin(im_C**0.5)**2

fig, axes = plt.subplots(2, 2, figsize = (10, 6), dpi = 100)
axes = axes.ravel()
locs = ['right', 'left',  'top', 'bottom']

im = (im_A, im_B, im_C)
for i, loc in enumerate(locs):
    cim = axes[i].pcolormesh(im, cmap='3VarAddA', vmax=(1, 1, 1))
    cb = fig.colorbar_multivar(cim, location=loc, n_major=2)
    cb[0].set_label('A')
    cb[1].set_label('B')
    cb[2].set_label('C')

fig.show()
image (only for illustration purposes, a user would need to handle the overlapping labels. Constrained layout (part 4) is a better solution for this particular layout)

AI Disclosure

The split of #31214 into smaller PRs, and the changes requested in review there, were
made with the assistance of an AI coding assistant (Claude). All changes were reviewed
and tested by me.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

trygvrad and others added 3 commits November 13, 2026 16:58
- Colorizer: an existing MultiNorm passed as *norm* is used as is instead
  of being wrapped in a new MultiNorm, which raised a ValueError.
- MultiNorm.autoscale_None only emits the 'changed' signal when the limits
  of a constituent norm actually changed.
- Colorbar.remove no longer fails when the mappable has no Axes.

Changes implemented with the assistance of AI and manually evaluated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add `Figure.colorbar_bivar` and `pyplot.colorbar_bivar`, which draw the
colorbar of a bivariate colormap as a 2D image, with one data variable
along each axis.

The new `BivarColorbar` class lives in the new module
`matplotlib.colorbar_multivariate`. Its Axes is created by the existing
`colorbar.make_axes` / `colorbar.make_axes_gridspec`, so it takes part in
layout (including constrained layout) like a regular colorbar, and
`colorbar.py` itself is not changed.

Changes implemented with the assistance of AI and manually evaluated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add `Figure.colorbar_multivar` and `pyplot.colorbar_multivar`, which draw
one regular colorbar per variable of a multivariate colormap. The
colorbars can be arranged in several rows or columns with *n_major*.

`MultivarColorbar` is a sequence of `Colorbar` objects and lives in
`matplotlib.colorbar_multivariate`. `make_multivar_axes` takes the space
for all colorbars from the parent Axes through `colorbar.make_axes`, as
for a single colorbar, and splits it into one Axes per variable.

Constrained layout is not supported yet and raises a NotImplementedError,
unless the Axes for the colorbars are passed in explicitly.

Changes implemented with the assistance of AI and manually evaluated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants