Issue 1:
Bug summary

The range of colors shown in the colorbar is incorrect when a TwoSlopeNorm is used over data which is biased to either side of the center of the Norm.

Here's the code for reproduction:

https://gist.github.com/bjodah/76d2f42684141f01ec350ad6d1edcd41

Code for reproduction

# see above link, or here's the pasted code from above notebook:
import matplotlib.pyplot as plt
import matplotlib.colors as mc
import numpy as np
def imshow_with_colorbar(M, cmap, norm=None):
fig, ax = plt.subplots(1, 1)
mappable = ax.imshow(M, cmap=cmap, norm=norm or mc.TwoSlopeNorm(0))
fig.colorbar(mappable)

A = np.arange(-3., 3.).reshape((2, 3))
B = np.arange(0.0, 6.).reshape((2, 3))

# This looks reasonable:
imshow_with_colorbar(A, plt.cm.coolwarm)

# when the range starts at zero, note how the color for zero (grey) is not represented in the colorbar:
imshow_with_colorbar(B, plt.cm.coolwarm)

# the bug is more apparent using another colormap (hsv):
imshow_with_colorbar(B, plt.cm.hsv)

# below is not a bug per se, just a possible area of improvement: the colorbar get no tick printed on negative side when magnitudes differ, so there is no way to estimate the magnitude by only looking at the plot:
B2 = B - 1e-10
imshow_with_colorbar(B2, plt.cm.coolwarm)

# one possible remedy is to manually set the lower bound to a value of simliar magnitude
m = np.max(np.abs(2))
imshow_with_colorbar(B2, plt.cm.coolwarm, norm=mc.TwoSlopeNorm(vmin=-m, vcenter=0, vmax=m))

# but then we cannot tell if we are in the negative or not by simply looking at the color

Actual outcome

(see above link for more context)

Expected outcome

the colorbar should include all colors shown in the axes.

Matplotlib Version

3.6.1

Python version

3.10

Issue 2:
Bug summary

When using TwoSlopeNorm with pcolormesh, I receive errors when I try to set the colorbar limits to both be on one side of the vcenter specified in TwoSlopeNorm.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

x = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
y = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
data = [[-1, -2], [-2, -3]]

plt.pcolormesh(x, y, data, cmap="coolwarm", norm=mcolors.TwoSlopeNorm(vmin=-3, vcenter=0, vmax=1))
plt.colorbar()

plt.clim([-3, -1]) # this breaks
# plt.clim([-3, 1]) # this works fine
# plt.clim([1, 3]) # this also breaks

Actual outcome

Traceback (most recent call last):
File "/home/eytan/Documents/matplotlib_debug.py", line 11, in <module>
plt.clim([-3, -1]) # this breaks
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/pyplot.py", line 2131, in clim
im.set_clim(vmin, vmax)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cm.py", line 525, in set_clim
self.norm.vmax = colors._sanitize_extrema(vmax)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colors.py", line 1161, in vmax
self._changed()
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colors.py", line 1178, in _changed
self.callbacks.process('changed')
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 292, in process
self.exception_handler(exc)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 96, in _exception_printer
raise exc
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 287, in process
func(*args, **kwargs)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cm.py", line 618, in changed
self.callbacks.process('changed', self)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 292, in process
self.exception_handler(exc)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 96, in _exception_printer
raise exc
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py", line 287, in process
func(*args, **kwargs)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py", line 551, in update_normal
self.draw_all()
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py", line 608, in draw_all
self._add_solids(X, Y, self._values[ind, np.newaxis])
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py", line 624, in _add_solids
self.solids = self.ax.pcolormesh(
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/__init__.py", line 1412, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 6058, in pcolormesh
X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
File "/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 5533, in _pcolorargs
raise ValueError(
ValueError: x and y arguments to pcolormesh cannot have non-finite values or be of type numpy.ma.core.MaskedArray with masked values

Expected outcome

A pcolormesh figure with a colorbar limit with a minimum of -3 and maximum of -1.

Additional information

Works in version 3.4.3, breaks in version 3.5.0. Could it be related to the fix for #21671 in #21676? It seems like a similar issue and also showed up in version 3.5.0.

Operating system

Ubuntu 20.04

Matplotlib Version

3.5.2

Matplotlib Backend

TkAgg

Python version

3.9.12

Jupyter version

No response

Installation

pip