Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
Updates colorbar.colorbar to accept a colorizer.Colorizer object, in addition to colorizer.ColorizingArtist. This commit also changes the docs from referencing cm.ScalarMappable
Another example where this PR is relevant is as follows: importmatplotlib.pyplotaspltimportnumpyasnpfig,axes=plt.subplots(1,3,figsize=(8,2))im0=axes[0].imshow(np.random.random((5,5)))colorizer=im0.colorizeraxes[1].imshow(2*np.random.random((5,5)),colorizer=colorizer)axes[2].imshow(0.5*np.random.random((5,5)),colorizer=colorizer)fig.colorbar(colorizer,ax=axes) Where a (This is similar tohttps://matplotlib.org/stable/gallery/images_contours_and_fields/multi_image.html , which I will make PR for following this PR) |
Just to be sure: Is this correct? - Previously colorbar must reference a ScalarMappable. If norm limits were not fixed, we use the ScalarMappable's data (if available) to autoscale, i.e. fix norm limits). The Colorizer now behaves like a scalar mappable without data. I'm not yet 100% convinced, we need colorbars to accept colorizers right now.
|
@timhoffm Thank you for considering this. While I would of course have like to see this included, I cannot fault the logic leading you to the conclusion that this is not required. In light of this, I have opened a new PR here, which simply updates the examples and documentation, but makes no changes to the API:#30112 Regarding auto-setting the norm, I am not convinced that it is worthwhile to seek a new solution here when multiple subplots are used, as I believe that best use is to set the limits manually, as is done in the example in the docs:https://matplotlib.org/stable/gallery/images_contours_and_fields/multi_image.html
And we are better served by steering users to that solution rather than complicating the default behavior. My opinion on this matter is informed by the fact that I find the auto-scaler to be fine for prototyping, but whenever I need to make a publication-quality or complicated figure, I find that I have to manually set the limits anyways. (i.e. the autoscaler sets limits to 0.007373 to 0.99633 when 0 and 1 and much more reasonable limits.) The convenience of the auto-scaler is great for simple plots, but once I have multiple plots that need to share a colorbar, I would argue that it is no longer a simple plot and the user can be expected to know how to set the limits according to their needs. |
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
andcolorizer.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.Considerthis example:
With the new colorizer API, one would expect be able to replace
cm.ScalarMappable
above withcolorizer.Colorizer
, however, this is currently not possible, and one must instead replacecm.ScalarMappable
above withcolorizer.ColorizingArtist
, which requires acolorizer.Colorizer
as input.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()
withincolorbar.Colorbar()
.This PR updates
colorbar.colorbar()
so that it can accept acolorizer.Colorizer
as an alternative tocolorizer.ColorizingArtist
.The following additional changes are included in this PR:
cm.ScalarMappable
tocolorizer.ColorizingArtist
.colorbar.Colorbar
to the new colorizer API by adding.colorizer
as a mutable property on acolorbar.Colorbar
[this must be mutable as it is mutable forColorizingArtist
and when it changes on the artist it must change on the colorbar].norm
and.cmap
of a colorbar properties that get the relevant property from the colorizer. This also makes the explicitly immutable..mappable
on acolorbar.Colorbar
explicitly immutable.PR checklist
¹ I would like to updatehttps://matplotlib.org/stable/users/explain/colors/colorbar_only.html andhttps://matplotlib.org/stable/gallery/images_contours_and_fields/multi_image.html but I think that would benefit from having this PR be implemented first.