8000 MNT: Use a context manager to change the norm in colorbar code by greglucas · Pull Request #22523 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MNT: Use a context manager to change the norm in colorbar code #22523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
End-users most likely won't need to directly use this module's API.
"""

import copy
import logging
import textwrap

import numpy as np

import matplotlib as mpl
from matplotlib import _api, collections, cm, colors, contour, ticker
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
import matplotlib.artist as martist
import matplotlib.patches as mpatches
import matplotlib.path as mpath
Expand Down Expand Up @@ -1154,20 +1153,24 @@ def _mesh(self):
These are scaled between vmin and vmax, and already handle colorbar
orientation.
"""
# copy the norm and change the vmin and vmax to the vmin and
# vmax of the colorbar, not the norm. This allows the situation
# where the colormap has a narrower range than the colorbar, to
# accommodate extra contours:
norm = copy.deepcopy(self.norm)
norm.vmin = self.vmin
norm.vmax = self.vmax
y, extendlen = self._proportional_y()
# invert:
if (isinstance(norm, (colors.BoundaryNorm, colors.NoNorm)) or
self.boundaries is not None):
y = y * (self.vmax - self.vmin) + self.vmin # not using a norm.
# Use the vmin and vmax of the colorbar, which may not be the same
# as the norm. There are situations where the colormap has a
# narrower range than the colorbar and we want to accommodate the
# extra contours.
if (isinstance(self.norm, (colors.BoundaryNorm, colors.NoNorm))
or self.boundaries is not None):
# not using a norm.
y = y * (self.vmax - self.vmin) + self.vmin
else:
y = norm.inverse(y)
# Update the norm values in a context manager as it is only
# a temporary change and we don't want to propagate any signals
# attached to the norm (callbacks.blocked).
with self.norm.callbacks.blocked(), \
cbook._setattr_cm(self.norm,
vmin=self.vmin,
vmax=self.vmax):
y = self.norm.inverse(y)
self._y = y
X, Y = np.meshgrid([0., 1.], y)
if self.orientation == 'vertical':
Expand Down
0