Skip to content

Support constrained layout for multivariate colorbars - #32608

Draft
trygvrad wants to merge 4 commits into
matplotlib:mainfrom
trygvrad:multivar-constrained-layout
Draft

trygvrad wants to merge 4 commits into
matplotlib:mainfrom
trygvrad:multivar-constrained-layout

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 4 of 4 and Depends on #32605, #32606 and #32607. Only the last commit ("Support constrained layout for multivariate colorbars") is new in this PR.

PR summary

  • Removes the NotImplementedError from #PR3: colorbar_multivar now works with layout='constrained'.
  • Follows jklymak's suggestion in Multivar colorbar #31214: the MultivarColorbar acts as a container with _colorbar_info and get_tightbbox, and is registered in the parent Axes' _colorbars like a colorbar Axes.
  • _constrained_layout.py: margins for a multivariate colorbar use its get_tightbbox(for_layout_only=True) (the size the whole grid needs); reposition_colorbar places the block like a single colorbar and then packs the individual colorbar Axes into a grid inside it (MultivarColorbar._get_tight_packing_inside).
  • The layout engine tells a gets the type of colorbar by _colorbar_info.get("type") == "MultivarColorbar"; regular colorbars are unaffected by the changes here.
  • Tests: test_colorbar_multivar_location_constrained and test_colorbar_multivar_n_major.

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, layout="constrained")
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

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
  • N/A Plotting related features are demonstrated in an example
  • N/A New features and API changes have release notes NOTE: covered by the release note of Multivar colorbar #32607.
  • Documentation complies with general and docstring guidelines

trygvrad and others added 4 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>
A MultivarColorbar created by `Figure.colorbar_multivar` now registers
itself with its parent Axes, and constrained layout treats the whole
block of colorbars like a single colorbar: it reserves space using the
tight bbox of the block and then places the individual colorbar Axes
in a grid inside the space it was given.

Changes implemented with the assistance of AI and manually evaluated

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

Copy link
Copy Markdown
Member

I think you've got a type and this is 4 of 4?

@trygvrad

Copy link
Copy Markdown
Contributor Author

I think you've got a type and this is 4 of 4?

yepp, thanks :)

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