Skip to content

Commit a5b443d

Browse files
committed
FIX: Use mappable data when autoscaling colorbar norm
When switching norms of the mappable we should autoscale the norm using the data if possible rather than setting it to (0, 1).
1 parent 78bf53c commit a5b443d

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,6 @@ def __init__(self, ax, mappable=None, *, cmap=None,
301301
if mappable is None:
302302
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
303303

304-
# Ensure the given mappable's norm has appropriate vmin and vmax
305-
# set even if mappable.draw has not yet been called.
306-
if mappable.get_array() is not None:
307-
mappable.autoscale_None()
308-
309304
self.mappable = mappable
310305
cmap = mappable.cmap
311306
norm = mappable.norm
@@ -1101,7 +1096,10 @@ def _process_values(self):
11011096
b = np.hstack((b, b[-1] + 1))
11021097

11031098
# transform from 0-1 to vmin-vmax:
1099+
if self.mappable.get_array() is not None:
1100+
self.mappable.autoscale_None()
11041101
if not self.norm.scaled():
1102+
# If we still aren't scaled after autoscaling, use 0, 1 as default
11051103
self.norm.vmin = 0
11061104
self.norm.vmax = 1
11071105
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(

lib/matplotlib/tests/test_colorbar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,12 @@ def test_colorbar_scale_reset():
657657

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

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

661667
def test_colorbar_get_ticks_2():
662668
plt.rcParams['_internal.classic_mode'] = False

0 commit comments

Comments
 (0)