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

Skip to content

Commit ab8de2d

Browse files
committed
Internal cleanup and fix locator _vcenter bug
1 parent 5935a2e commit ab8de2d

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

‎proplot/axes/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ def _add_colorbar(
10831083
# DiscreteLocator or else get issues (see mpl #22233).
10841084
norm = mappable.norm
10851085
source = getattr(norm, '_norm', None)
1086-
vcenter = {'vcenter': getattr(source, 'vcenter', 0.0)}
1086+
vcenter = getattr(source, 'vcenter', None)
1087+
vcenter = {} if vcenter is None else {'vcenter': vcenter}
10871088
formatter = _not_none(formatter, getattr(norm, '_labels', None), 'auto')
10881089
formatter = constructor.Formatter(formatter, **formatter_kw)
10891090
categorical = isinstance(formatter, mticker.FixedFormatter)

‎proplot/axes/plot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,7 @@ def _parse_level_num(
25002500
Unused arguments.
25012501
"""
25022502
# Input args
2503-
# NOTE: Some of this is adapted from the hidden contour.ContourSet._autolev
2503+
# NOTE: Some of this is adapted from contour.ContourSet._autolev
25042504
# NOTE: We use 'symmetric' with MaxNLocator to ensure boundaries
25052505
# include a zero level but may trim many of these levels below.
25062506
norm_kw = norm_kw or {}
@@ -2551,7 +2551,7 @@ def _parse_level_num(
25512551
vmin, vmax, levels = vmin + vcenter, vmax + vcenter, levels + vcenter
25522552

25532553
# Possibly trim levels far outside of 'vmin' and 'vmax'
2554-
# NOTE: This part is mostly copied from contour.ContourSet._autolev
2554+
# NOTE: This part is mostly copied from matplotlib _autolev
25552555
if not symmetric:
25562556
i0, i1 = 0, len(levels) # defaults
25572557
under, = np.where(levels < vmin)
@@ -2832,7 +2832,11 @@ def _parse_level_norm(
28322832

28332833
# Generate DiscreteNorm and update "child" norm with vmin and vmax from
28342834
# levels. This lets the colorbar set tick locations properly!
2835-
if not isinstance(norm, mcolors.BoundaryNorm) and len(levels) > 1:
2835+
if len(levels) == 1:
2836+
pass # e.g. contours
2837+
elif isinstance(norm, mcolors.BoundaryNorm):
2838+
pass # override with native matplotlib normalizer
2839+
else:
28362840
norm = pcolors.DiscreteNorm(
28372841
levels, norm=norm, unique=unique, step=step,
28382842
ticks=discrete_ticks, labels=discrete_labels,

‎proplot/colors.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,19 +2440,17 @@ def __init__(
24402440
# minimum 0 maximum 1, would mess up color distribution. However this is still
24412441
# not perfect... get asymmetric color intensity either side of central point.
24422442
# So we add special handling for diverging norms below to improve symmetry.
2443-
if len(levels) == 2:
2444-
step = 0.5 # dummy step
2445-
mids[0] += step * (levels[0] - levels[1])
2446-
mids[-1] += step * (levels[-1] - levels[-2])
2447-
else:
2448-
if unique in ('min', 'both'):
2449-
offset = mids[1] - mids[2]
2450-
mids[0] += step * offset
2451-
if unique in ('max', 'both'):
2452-
offset = mids[-2] - mids[-3]
2453-
mids[-1] += step * offset
2443+
if unique in ('min', 'both'):
2444+
scale = levels[0] - levels[1] if len(levels) == 2 else mids[1] - mids[2]
2445+
mids[0] += step * scale
2446+
if unique in ('max', 'both'):
2447+
scale = levels[-1] - levels[-2] if len(levels) == 2 else mids[-2] - mids[-3]
2448+
mids[-1] += step * scale
24542449
mmin = np.min(mids)
24552450
mmax = np.max(mids)
2451+
if np.isclose(mmin, mmax):
2452+
mmin = mmin - (mmin or 1) * 1e-10
2453+
mmax = mmax + (mmax or 1) * 1e-10
24562454
if vcenter is None: # not diverging norm or centered segmented norm
24572455
mids = _interpolate_scalar(mids, mmin, mmax, vmin, vmax)
24582456
else:

0 commit comments

Comments
 (0)