diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index d3d53e3d98f8..f8235a5a08d7 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -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 @@ -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() 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( diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index e39d0073786b..9e28e37773f5 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -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