Skip to content

Commit f83570c

Browse files
committed
Allow BoundaryNorm passed to cmap_changer (fix colorbar bug)
1 parent 3502eb5 commit f83570c

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

‎proplot/wrappers.py‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,17 +2073,17 @@ def cmap_changer(
20732073
nlevels.append(olevels[-1])
20742074
levels = norm.inverse(nlevels)
20752075

2076-
# Norm settings
2077-
# Generate BinNorm and update child norm object with vmin/vmax from levels
2076+
# Generate BinNorm and update "child" norm with vmin and vmax from levels
20782077
# This is important for the colorbar setting tick locations properly!
20792078
if norm is not None:
2080-
if levels is not None:
2081-
norm.vmin, norm.vmax = min(levels), max(levels)
2082-
if levels is not None:
2083-
bin_kw = {'extend': extend}
2084-
if cyclic:
2085-
bin_kw.update({'step': 0.5, 'extend': 'both'})
2086-
norm = styletools.BinNorm(norm=norm, levels=levels, **bin_kw)
2079+
if not isinstance(norm, mcolors.BoundaryNorm):
2080+
if levels is not None:
2081+
norm.vmin, norm.vmax = min(levels), max(levels)
2082+
if levels is not None:
2083+
bin_kw = {'extend': extend}
2084+
if cyclic:
2085+
bin_kw.update({'step': 0.5, 'extend': 'both'})
2086+
norm = styletools.BinNorm(norm=norm, levels=levels, **bin_kw)
20872087
kwargs['norm'] = norm
20882088

20892089
# Call function
@@ -2098,12 +2098,15 @@ def cmap_changer(
20982098
if locator is not None and not isinstance(locator, mticker.MaxNLocator):
20992099
obj.locator = locator # for colorbar to determine tick locations
21002100

2101-
# Call again for contourf plots with edges
2102-
if 'contourf' in name and (linewidths is not None or colors is not None
2103-
or linestyles is not None):
2101+
# Call again to add "edges" to contourf plots
2102+
if 'contourf' in name and any(
2103+
_ is not None for _ in (colors, linewidths, linestyles)
2104+
):
21042105
colors = _notNone(colors, 'k')
2105-
cobj = self.contour(*args, levels=levels, linewidths=linewidths,
2106-
linestyles=linestyles, colors=colors)
2106+
self.contour(
2107+
*args, levels=levels, linewidths=linewidths,
2108+
linestyles=linestyles, colors=colors
2109+
)
21072110

21082111
# Apply labels
21092112
# TODO: Add quiverkey to this!
@@ -2124,8 +2127,7 @@ def cmap_changer(
21242127
cobj = obj
21252128
colors = None
21262129
text_kw = {}
2127-
for key in (
2128-
*labels_kw,): # allow dict to change size during iteration
2130+
for key in (*labels_kw,): # allow dict to change size
21292131
if key not in (
21302132
'levels', 'fontsize', 'colors', 'inline', 'inline_spacing',
21312133
'manual', 'rightside_up', 'use_clabeltext',
@@ -2950,7 +2952,8 @@ def colorbar_wrapper(
29502952
if tickminor:
29512953
cb.minorticks_on()
29522954
else:
2953-
# Private API is the only way!
2955+
# Set the minor ticks just like matplotlib internally sets the
2956+
# major ticks. Private API is the only way!
29542957
minorlocator = axistools.Locator(minorlocator, **minorlocator_kw)
29552958
ticks, *_ = cb._ticker(minorlocator, mticker.NullFormatter())
29562959
axis.set_ticks(ticks, minor=True)

0 commit comments

Comments
 (0)