Skip to content

Update colorbar with new colorizer API #30008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

trygvrad
Copy link
Contributor

@trygvrad trygvrad commented May 4, 2025

Updates colorbar.colorbar to accept a colorizer.Colorizer object, in addition to colorizer.ColorizingArtist. This commit also changes the docs from referencing cm.ScalarMappable to referencing colorizer.ColorizingArtist.

PR summary

With the introduction of colorizer.Colorizer and colorizer.ColorizingArtist (#28658) we have separated the norm→color pipeline from the artist. However, the colorbar API has not been updated since these changes took effect.

Consider this example:

fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')

norm = mpl.colors.Normalize(vmin=5, vmax=10)

fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap='cool'),
             cax=ax, orientation='horizontal', label='Some Units')

image

With the new colorizer API, one would expect be able to replace cm.ScalarMappable above with colorizer.Colorizer, however, this is currently not possible, and one must instead replace cm.ScalarMappable above with colorizer.ColorizingArtist, which requires a colorizer.Colorizer as input.

fig.colorbar(mpl.colorizer.ColorizingArtist(mpl.colorizer.Colorizer(norm=norm, cmap='cool')),
             cax=ax, orientation='horizontal', label='Some Units')

This is despite the fact that the norm→color pipeline is entirely contained in the colorizer, and it fails only because of a single call to self.mappable.get_array() within colorbar.Colorbar().

This PR updates colorbar.colorbar() so that it can accept a colorizer.Colorizer as an alternative to colorizer.ColorizingArtist.

fig.colorbar(mpl.colorizer.Colorizer(norm=norm, cmap='cool'),
             cax=ax, orientation='horizontal', label='Some Units')

The following additional changes are included in this PR:

  • Changes the docs from referencing cm.ScalarMappable to colorizer.ColorizingArtist.
  • Updates colorbar.Colorbar to the new colorizer API by adding .colorizer as a mutable property on a colorbar.Colorbar [this must be mutable as it is mutable for ColorizingArtist and when it changes on the artist it must change on the colorbar]
  • makes the .norm and .cmap of a colorbar properties that get the relevant property from the colorizer. This also makes the explicitly immutable.
  • makes the .mappable on a colorbar.Colorbar explicitly immutable.

PR checklist

¹ I would like to update https://matplotlib.org/stable/users/explain/colors/colorbar_only.html and https://matplotlib.org/stable/gallery/images_contours_and_fields/multi_image.html but I think that would benefit from having this PR be implemented first.

Updates colorbar.colorbar to accept a colorizer.Colorizer object, in addition to colorizer.ColorizingArtist. This commit also changes the docs from referencing cm.ScalarMappable
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.

1 participant