diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 72f30723ae64..ec37014a74be 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -584,9 +584,8 @@ def _use_auto_colorbar_locator(self): Return if we should use an adjustable tick locator or a fixed one. (check is used twice so factored out here...) """ - contouring = self.boundaries is not None and self.spacing == 'uniform' - return (type(self.norm) in [colors.Normalize, colors.LogNorm] - and not contouring) + # contouring = self.boundaries is not None and self.spacing == 'uniform' + return (type(self.norm) in [colors.Normalize, colors.LogNorm]) def _reset_locator_formatter_scale(self): """ @@ -1119,11 +1118,11 @@ def _mesh(self): else: y = self._proportional_y() xmid = np.array([0.5]) - try: + if self._use_auto_colorbar_locator(): y = norm.inverse(y) x = norm.inverse(x) xmid = norm.inverse(xmid) - except ValueError: + else: # occurs for norms that don't have an inverse, in # which case manually scale: dv = self.vmax - self.vmin diff --git a/lib/matplotlib/tests/baseline_images/test_colorbar/colorbar_norms.png b/lib/matplotlib/tests/baseline_images/test_colorbar/colorbar_norms.png new file mode 100644 index 000000000000..922ecc0519f6 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_colorbar/colorbar_norms.png differ diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 3932177565c5..7c6a9100843f 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -373,7 +373,6 @@ def test_colorbar_get_ticks(): data = np.arange(1200).reshape(30, 40) levels = [0, 200, 400, 600, 800, 1000, 1200] - plt.subplot() plt.contourf(data, levels=levels) # testing getter for user set ticks @@ -581,3 +580,18 @@ def test_colorbar_int(clim): im = ax.imshow([[*map(np.int16, clim)]]) fig.colorbar(im) assert (im.norm.vmin, im.norm.vmax) == clim + + +@image_comparison( + baseline_images=['colorbar_norms'], extensions=['png']) +def test_cbar_norms(): + # Test colormaps with different norms + norms = [mcolors.Normalize(vmin=-1, vmax=2), + mcolors.BoundaryNorm(boundaries=[-1, 0, 1], ncolors=4), + mcolors.LogNorm(vmin=1, vmax=1e4), + mcolors.PowerNorm(gamma=0.2, vmin=0.1, vmax=0.6), + mcolors.SymLogNorm(1, vmin=-10, vmax=10), + mcolors.TwoSlopeNorm(1, vmin=0, vmax=2)] + fig, axs = plt.subplots(ncols=len(norms), constrained_layout=True) + for ax, norm in zip(axs, norms): + fig.colorbar(cm.ScalarMappable(norm=norm, cmap='viridis'), cax=ax, extend='both')