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

Skip to content

Commit e175ffc

Browse files
dstansbyjklymak
authored andcommitted
FIX colorbars for Norms that do not have a scale.
1 parent 7a2971b commit e175ffc

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/matplotlib/colorbar.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def __init__(self, ax, cmap=None,
452452
self.locator = None
453453
self.formatter = None
454454
self._manual_tick_data_values = None
455+
self._scale = None # linear, log10 for now. Hopefully more?
455456

456457
if ticklocation == 'auto':
457458
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
@@ -585,8 +586,8 @@ def _use_auto_colorbar_locator(self):
585586
one. (check is used twice so factored out here...)
586587
"""
587588
contouring = self.boundaries is not None and self.spacing == 'uniform'
588-
return (type(self.norm) in [colors.Normalize, colors.LogNorm]
589-
and not contouring)
589+
return (type(self.norm) in [colors.Normalize, colors.LogNorm] and
590+
not contouring)
590591

591592
def _reset_locator_formatter_scale(self):
592593
"""
@@ -602,9 +603,16 @@ def _reset_locator_formatter_scale(self):
602603
self.ax.set_xscale('log')
603604
self.ax.set_yscale('log')
604605
self.minorticks_on()
606+
self._scale = 'log'
605607
else:
606608
self.ax.set_xscale('linear')
607609
self.ax.set_yscale('linear')
610+
if (isinstance(self.norm, colors.Normalize) and
611+
not issubclass(type(self.norm), colors.Normalize)):
612+
self._scale = 'linear'
613+
else:
614+
self._scale = 'manual'
615+
608616

609617
def update_ticks(self):
610618
"""
@@ -1119,13 +1127,13 @@ def _mesh(self):
11191127
else:
11201128
y = self._proportional_y()
11211129
xmid = np.array([0.5])
1122-
try:
1130+
if not self._scale == 'manual':
11231131
y = norm.inverse(y)
11241132
x = norm.inverse(x)
11251133
xmid = norm.inverse(xmid)
1126-
except ValueError:
1127-
# occurs for norms that don't have an inverse, in
1128-
# which case manually scale:
1134+
else:
1135+
# if a norm doesn't have a named scale, or
1136+
# we are not using a norm
11291137
dv = self.vmax - self.vmin
11301138
x = x * dv + self.vmin
11311139
y = y * dv + self.vmin

lib/matplotlib/tests/test_colorbar.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ def test_colorbar_get_ticks():
373373
data = np.arange(1200).reshape(30, 40)
374374
levels = [0, 200, 400, 600, 800, 1000, 1200]
375375

376-
plt.subplot()
377376
plt.contourf(data, levels=levels)
378377

379378
# testing getter for user set ticks
@@ -581,3 +580,18 @@ def test_colorbar_int(clim):
581580
im = ax.imshow([[*map(np.int16, clim)]])
582581
fig.colorbar(im)
583582
assert (im.norm.vmin, im.norm.vmax) == clim
583+
584+
585+
@image_comparison(
586+
baseline_images=['colorbar_norms'], extensions=['png'])
587+
def test_cbar_norms():
588+
# Test colormaps with different norms
589+
norms = [mcolors.Normalize(vmin=-1, vmax=2),
590+
mcolors.BoundaryNorm(boundaries=[-1, 0, 1], ncolors=4),
591+
mcolors.LogNorm(vmin=1, vmax=1e4),
592+
mcolors.PowerNorm(gamma=0.2, vmin=0.1, vmax=0.6),
593+
mcolors.SymLogNorm(1, vmin=-10, vmax=10),
594+
mcolors.TwoSlopeNorm(1, vmin=0, vmax=2)]
595+
fig, axs = plt.subplots(ncols=len(norms), constrained_layout=True)
596+
for ax, norm in zip(axs, norms):
597+
fig.colorbar(cm.ScalarMappable(norm=norm, cmap='viridis'), cax=ax, extend='both')

0 commit comments

Comments
 (0)