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

Skip to content

Commit d21b9dc

Browse files
authored
Merge pull request #25498 from greglucas/cbar-new-norm
FIX: Use mappable data when autoscaling colorbar norm
2 parents c5abf90 + a5b443d commit d21b9dc

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
@@ -1091,7 +1086,10 @@ def _process_values(self):
10911086
b = np.hstack((b, b[-1] + 1))
10921087

10931088
# transform from 0-1 to vmin-vmax:
1089+
if self.mappable.get_array() is not None:
1090+
self.mappable.autoscale_None()
10941091
if not self.norm.scaled():
1092+
# If we still aren't scaled after autoscaling, use 0, 1 as default
10951093
self.norm.vmin = 0
10961094
self.norm.vmax = 1
10971095
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
@@ -653,6 +653,12 @@ def test_colorbar_scale_reset():
653653

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

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

657663
def test_colorbar_get_ticks_2():
658664
plt.rcParams['_internal.classic_mode'] = False

0 commit comments

Comments
 (0)