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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,6 @@ def __init__(self, ax, mappable=None, *, cmap=None,
if mappable is None:
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)

# Ensure the given mappable's norm has appropriate vmin and vmax
# set even if mappable.draw has not yet been called.
if mappable.get_array() is not None:
mappable.autoscale_None()

self.mappable = mappable
cmap = mappable.cmap
norm = mappable.norm
Expand Down Expand Up @@ -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.

if not self.norm.scaled():
# If we still aren't scaled after autoscaling, use 0, 1 as default
self.norm.vmin = 0
self.norm.vmax = 1
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,12 @@ def test_colorbar_scale_reset():

assert cbar.outline.get_edgecolor() == mcolors.to_rgba('red')

# log scale with no vmin/vmax set should scale to the data if there
# is a mappable already associated with the colorbar, not (0, 1)
pcm.norm = LogNorm()
assert pcm.norm.vmin == z.min()
assert pcm.norm.vmax == z.max()


def test_colorbar_get_ticks_2():
plt.rcParams['_internal.classic_mode'] = False
Expand Down