Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Use mappable data when autoscaling colorbar norm #25498

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

Merged
merged 1 commit into from
Mar 29, 2023

Conversation

greglucas
Copy link
Contributor

PR Summary

When switching norms of the mappable we should autoscale the norm using the data if possible rather than setting it to (0, 1). From the linked issue, this currently errors due to vmin=0 when updating the norm, even though the data is all valid for LogNorm.

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm, Normalize
import numpy as np

img = np.arange(1, 11).reshape((2, 5))

fig, ax = plt.subplots(1, 1)
plot = ax.imshow(img, cmap='gray')
cb = fig.colorbar(plot, ax=ax)
plot.norm = LogNorm()
plt.show()

closes #5424

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • [-] Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • [-] New plotting related features are documented with examples.

Release Notes

  • [-] New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • [-] API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • [-] Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

When switching norms of the mappable we should autoscale the norm
using the data if possible rather than setting it to (0, 1).
@greglucas greglucas added this to the v3.8.0 milestone Mar 18, 2023
@@ -1101,7 +1096,10 @@ def _process_values(self):
b = np.hstack((b, b[-1] + 1))

# transform from 0-1 to vmin-vmax:
if self.mappable.get_array() is not None:
self.mappable.autoscale_None()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is getting called every draw, do we need to optimize autoscale_None at all? Currently it calls np.asanyarray on the data - I;'m not sure if that is expensive or not, but if it is, then we should only do that if we need to...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.asanyarray should fast-return anything that is vaguely numpy array and in mappable.set_array() we do cast the input through np.aray so I think e should be ok.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure of my performance analysis, but holding off merging to make sure @jklymak is convinced.

@jklymak
Copy link
Member

jklymak commented Mar 29, 2023

No I was just asking out of ignorance - this moves the normalization from startup to draw time, so it would be preferable if it were efficient.

@jklymak jklymak merged commit d21b9dc into matplotlib:main Mar 29, 2023
@greglucas greglucas deleted the cbar-new-norm branch March 29, 2023 20:58
@greglucas
Copy link
Contributor Author

Hopefully, people aren't updating their colorbars and redrawing frequently... (Says the person who added the pan/zoom options to the colorbar 😂 )

Good question though, I suppose if someone is paranoid, we could add an even sooner check on the if self.vmin is None or self.vmax is None before hitting that.

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.

Update colorbar after changing mappable.norm
4 participants