Skip to content

Commit 8759fe6

Browse files
committed
FIX: allow norm limit to change and not change user-set ticks and format
1 parent 2aa350e commit 8759fe6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/matplotlib/colorbar.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1212,16 +1212,20 @@ def update_normal(self, mappable):
12121212
meant to be called when the norm of the image or contour plot to which
12131213
this colorbar belongs changes.
12141214
1215-
This resets the locator and formatter for the axis, so if these
1216-
have been customized, they will need to be customized again.
1215+
If the norm on the mappable is different than before, this resets the
1216+
locator and formatter for the axis, so if these have been customized,
1217+
they will need to be customized again. However, if the norm only
1218+
changes values of *vmin*, *vmax* or *cmap* then the old formatter
1219+
and locator will be preserved.
12171220
"""
12181221

1219-
_log.debug('colorbar update normal')
1222+
_log.debug('colorbar update normal %r %r', mappable.norm, self.norm)
12201223
self.mappable = mappable
12211224
self.set_alpha(mappable.get_alpha())
12221225
self.cmap = mappable.cmap
1223-
self.norm = mappable.norm
1224-
self._reset_locator_formatter_scale()
1226+
if mappable.norm != self.norm:
1227+
self.norm = mappable.norm
1228+
self._reset_locator_formatter_scale()
12251229

12261230
self.draw_all()
12271231
if isinstance(self.mappable, contour.ContourSet):

lib/matplotlib/tests/test_colorbar.py

+12
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,18 @@ def test_colorbar_format():
477477
fig.canvas.draw()
478478
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '6.00e+04'
479479

480+
# make sure that if we change the clim of the mappable that the
481+
# formatting is *not* lost:
482+
im.set_clim([4, 200])
483+
fig.canvas.draw()
484+
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '8.00e+01'
485+
486+
# but if we change the norm:
487+
im.set_norm(LogNorm(vmin=0.1, vmax=10))
488+
fig.canvas.draw()
489+
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
490+
r'$\mathdefault{10^{-1}}$')
491+
480492

481493
def test_colorbar_scale_reset():
482494
x, y = np.ogrid[-4:4:31j, -4:4:31j]

0 commit comments

Comments
 (0)