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

Skip to content

Commit 7d35771

Browse files
committed
MNT: Use a context manager to change the norm in colorbar code
This removes a deepcopy of the norm in Colorbar, instead updating the vmin/vmax via the context manager and ignoring any callback updates in the process.
1 parent 81e9559 commit 7d35771

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

lib/matplotlib/colorbar.py

Lines changed: 17 additions & 18 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
@@ -1158,22 +1157,22 @@ def _mesh(self):
11581157
# vmax of the colorbar, not the norm. This allows the situation
11591158
# where the colormap has a narrower range than the colorbar, to
11601159
# accommodate extra contours:
1161-
norm = copy.deepcopy(self.norm)
1162-
norm.vmin = self.vmin
1163-
norm.vmax = self.vmax
1164-
y, extendlen = self._proportional_y()
1165-
# invert:
1166-
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
1167-
self.boundaries is not None):
1168-
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
1169-
else:
1170-
y = norm.inverse(y)
1171-
self._y = y
1172-
X, Y = np.meshgrid([0., 1.], y)
1173-
if self.orientation == 'vertical':
1174-
return (X, Y, extendlen)
1175-
else:
1176-
return (Y, X, extendlen)
1160+
with self.norm.callbacks.blocked(), \
1161+
cbook._setattr_cm(self.norm, vmin=self.vmin, vmax=self.vmax):
1162+
y, extendlen = self._proportional_y()
1163+
# invert:
1164+
if (isinstance(self.norm, (colors.BoundaryNorm, colors.NoNorm))
1165+
or self.boundaries is not None):
1166+
# not using a norm.
1167+
y = y * (self.vmax - self.vmin) + self.vmin
1168+
else:
1169+
y = self.norm.inverse(y)
1170+
self._y = y
1171+
X, Y = np.meshgrid([0., 1.], y)
1172+
if self.orientation == 'vertical':
1173+
return (X, Y, extendlen)
1174+
else:
1175+
return (Y, X, extendlen)
11771176

11781177
def _forward_boundaries(self, x):
11791178
# map boundaries equally between 0 and 1...

0 commit comments

Comments
 (0)