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

Skip to content

Commit 23cd7dd

Browse files
authored
Merge pull request #22523 from greglucas/cbar-rm-norm-deepcopy
MNT: Use a context manager to change the norm in colorbar code
2 parents 2c463fe + fdb4ad3 commit 23cd7dd

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

lib/matplotlib/colorbar.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
End-users most likely won't need to directly use this module's API.
1212
"""
1313

14-
import copy
1514
import logging
1615
import textwrap
1716

1817
import numpy as np
1918

2019
import matplotlib as mpl
21-
from matplotlib import _api, collections, cm, colors, contour, ticker
20+
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
2221
import matplotlib.artist as martist
2322
import matplotlib.patches as mpatches
2423
import matplotlib.path as mpath
@@ -1152,20 +1151,24 @@ def _mesh(self):
11521151
These are scaled between vmin and vmax, and already handle colorbar
11531152
orientation.
11541153
"""
1155-
# copy the norm and change the vmin and vmax to the vmin and
1156-
# vmax of the colorbar, not the norm. This allows the situation
1157-
# where the colormap has a narrower range than the colorbar, to
1158-
# accommodate extra contours:
1159-
norm = copy.deepcopy(self.norm)
1160-
norm.vmin = self.vmin
1161-
norm.vmax = self.vmax
11621154
y, extendlen = self._proportional_y()
1163-
# invert:
1164-
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
1165-
self.boundaries is not None):
1166-
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
1155+
# Use the vmin and vmax of the colorbar, which may not be the same
1156+
# as the norm. There are situations where the colormap has a
1157+
# narrower range than the colorbar and we want to accommodate the
1158+
# extra contours.
1159+
if (isinstance(self.norm, (colors.BoundaryNorm, colors.NoNorm))
1160+
or self.boundaries is not None):
1161+
# not using a norm.
1162+
y = y * (self.vmax - self.vmin) + self.vmin
11671163
else:
1168-
y = norm.inverse(y)
1164+
# Update the norm values in a context manager as it is only
1165+
# a temporary change and we don't want to propagate any signals
1166+
# attached to the norm (callbacks.blocked).
1167+
with self.norm.callbacks.blocked(), \
1168+
cbook._setattr_cm(self.norm,
1169+
vmin=self.vmin,
1170+
vmax=self.vmax):
1171+
y = self.norm.inverse(y)
11691172
self._y = y
11701173
X, Y = np.meshgrid([0., 1.], y)
11711174
if self.orientation == 'vertical':

0 commit comments

Comments
 (0)