Skip to content

Adding colorbar changes imshow plot color when vmin is close to vmax #5467

Closed as not planned

Description

@IamJeffG

I can't tell whether this is an intended feature or a bug, so I'm going to run it by the group.

f = plt.figure()
mappable = plt.gca().imshow([[42]])

Here, we gave a single pixel, so the entire plot is the same color.
With default jet colormap, that single color is dark blue (the bottom of the jet cmap). So far so good.

f.colorbar(mappable)

Here, observe that since vmin==vmax (effectively), the colorbar defaulted to the midpoint of the colormap, now green. Luckily, the Axes from imshow, containing the data, changed too!

On one hand, I like that the colorbar shows context around the singular value. (I think due to this line.) On the other hand, over in Geopandas we plot patches one-by-one with face_color values from a normalized colormap, but adding this renormalization means the colorbar no longer matches the cmap I passed it.

Thoughts? If you tell me this is intended I'm willing to find a workaround to my use case. (Frankly I'm not 100% sure what I would expect matplotlib to do here instead.) Just wanted to get a second opinion.

Activity

  1. mdboom commented on Nov 12, 2015

    @mdboom
    Member
  2. added this to the milestone on Nov 12, 2015
  3. tacaswell commented on Nov 12, 2015

    @tacaswell
    Member

    We probably should push the range expansion logic up (down?) into ColorMappable.

    @IamJeffG You might be better performance if you use PatchCollection and we will do the color mapping for you.

  4. IamJeffG commented on Jan 10, 2016

    @IamJeffG
    Author

    @tacaswell Thanks for the PatchCollections tip, they do give better performance! But sometimes we'll have a GeoSeries with multiple geometry types, requiring multiple Collections. I can't find a way to have two different Collections share the same colormap using collection.set_array(), so I still end up explicitly setting the colors on each Collection to the result of mappable.to_rgba(values) -- which still has the bug mentioned above.

  5. modified the milestones: , on Feb 1, 2016
  6. tacaswell commented on Feb 1, 2016

    @tacaswell
    Member

    @IamJeffG mappable.set_map(cm) does not work?

  7. IamJeffG commented on Feb 16, 2016

    @IamJeffG
    Author

    @tacaswell Each collection re-normalizes the colormap to its own range of values. So if collection1 has values in (0, 5) and collection2 has values in (20, 25), each with set_map('RdBu'), I wanted collection1 (low values) to be in the reds and collection2 (high values) to be in the blues.

    I was able to do this by setting set_clim to the absolute min and max on both collections. Problem solved! (The behavior mentioned in the issue title is still a problem though.)

  8. modified the milestones: , on May 3, 2017
  9. modified the milestones: , on Oct 9, 2017
  10. github-actions commented on Jun 5, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  11. IamJeffG commented on Jun 8, 2023

    @IamJeffG
    Author

    I still notice the behavior from the issue title ("adding colorbar changes imshow plot color") but I'm not convinced if this behavior is buggy. Let's let this go. Closing.

  12. removed this from the future releases milestone on Jun 8, 2023
  13. tacaswell commented on Jun 8, 2023

    @tacaswell
    Member

    Thanks @IamJeffG .

    Re-reading this, I think I agree. We are in a bit of a bind. If you want a colorbar, the norm must not be singular so our options are to expand the norm or to raise and I think this is a case where expanding is reasonable.

    Currently if you set the norm to be singular after the colorbar is added it will re-expand itself.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions