<h3 dir="auto">Bug summary</h3>
<p dir="auto">The range of colors shown in the colorbar is incorrect when a <code class="notranslate">TwoSlopeNorm</code> is used over data which is biased to either side of the center of the Norm.</p>
<p dir="auto">Here's the code for reproduction:</p>
<p dir="auto"><a href="https://gist.github.com/bjodah/76d2f42684141f01ec350ad6d1edcd41">https://gist.github.com/bjodah/76d2f42684141f01ec350ad6d1edcd41</a></p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# 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"><pre class="notranslate"><span class="pl-c"># see above link, or here's the pasted code from above notebook:</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">colors</span> <span class="pl-k">as</span> <span class="pl-s1">mc</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">def</span> <span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">M</span>, <span class="pl-s1">cmap</span>, <span class="pl-s1">norm</span><span class="pl-c1">=</span><span class="pl-c1">None</span>):
    <span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-c1">1</span>, <span class="pl-c1">1</span>)
    <span class="pl-s1">mappable</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-v">M</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">cmap</span>, <span class="pl-s1">norm</span><span class="pl-c1">=</span><span class="pl-s1">norm</span> <span class="pl-c1">or</span> <span class="pl-s1">mc</span>.<span class="pl-v">TwoSlopeNorm</span>(<span class="pl-c1">0</span>))
    <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">mappable</span>)



<span class="pl-v">A</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">-</span><span class="pl-c1">3.</span>, <span class="pl-c1">3.</span>).<span class="pl-en">reshape</span>((<span class="pl-c1">2</span>, <span class="pl-c1">3</span>))
<span class="pl-v">B</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0.0</span>, <span class="pl-c1">6.</span>).<span class="pl-en">reshape</span>((<span class="pl-c1">2</span>, <span class="pl-c1">3</span>))

<span class="pl-c"># This looks reasonable:</span>
<span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">A</span>, <span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">coolwarm</span>)

<span class="pl-c"># when the range starts at zero, note how the color for zero (grey) is not represented in the colorbar:</span>
<span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">B</span>, <span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">coolwarm</span>)

<span class="pl-c"># the bug is more apparent using another colormap (hsv):</span>
<span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">B</span>, <span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">hsv</span>)

<span class="pl-c"># 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:</span>
<span class="pl-v">B2</span> <span class="pl-c1">=</span> <span class="pl-v">B</span> <span class="pl-c1">-</span> <span class="pl-c1">1e-10</span>
<span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">B2</span>, <span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">coolwarm</span>)

<span class="pl-c"># one possible remedy is to manually set the lower bound to a value of simliar magnitude</span>
<span class="pl-s1">m</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">max</span>(<span class="pl-s1">np</span>.<span class="pl-en">abs</span>(<span class="pl-c1">2</span>))
<span class="pl-en">imshow_with_colorbar</span>(<span class="pl-v">B2</span>, <span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">coolwarm</span>, <span class="pl-s1">norm</span><span class="pl-c1">=</span><span class="pl-s1">mc</span>.<span class="pl-v">TwoSlopeNorm</span>(<span class="pl-s1">vmin</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-s1">m</span>, <span class="pl-s1">vcenter</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">vmax</span><span class="pl-c1">=</span><span class="pl-s1">m</span>))

<span class="pl-c"># but then we cannot tell if we are in the negative or not by simply looking at the color</span></pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/485936/198277875-ec22b48c-877a-449e-8fbb-89fe528315e6.png"><img src="https://user-images.githubusercontent.com/485936/198277875-ec22b48c-877a-449e-8fbb-89fe528315e6.png" alt="bild" style="max-width: 100%;"></a><br>
(see above link for more context)</p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">the colorbar should include all colors shown in the axes.</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.1</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.10</p>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">When using <code class="notranslate">TwoSlopeNorm</code> with <code class="notranslate">pcolormesh</code>, I receive errors when I try to set the colorbar limits to both be on one side of the <code class="notranslate">vcenter</code> specified in <code class="notranslate">TwoSlopeNorm</code>.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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=&quot;coolwarm&quot;, 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"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">colors</span> <span class="pl-k">as</span> <span class="pl-s1">mcolors</span>

<span class="pl-s1">x</span> <span class="pl-c1">=</span> [[<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>]]
<span class="pl-s1">y</span> <span class="pl-c1">=</span> [[<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>], [<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>], [<span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>]]
<span class="pl-s1">data</span> <span class="pl-c1">=</span> [[<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">-</span><span class="pl-c1">2</span>], [<span class="pl-c1">-</span><span class="pl-c1">2</span>, <span class="pl-c1">-</span><span class="pl-c1">3</span>]]

<span class="pl-s1">plt</span>.<span class="pl-en">pcolormesh</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>, <span class="pl-s1">data</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s">"coolwarm"</span>, <span class="pl-s1">norm</span><span class="pl-c1">=</span><span class="pl-s1">mcolors</span>.<span class="pl-v">TwoSlopeNorm</span>(<span class="pl-s1">vmin</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">3</span>, <span class="pl-s1">vcenter</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">vmax</span><span class="pl-c1">=</span><span class="pl-c1">1</span>))
<span class="pl-s1">plt</span>.<span class="pl-en">colorbar</span>()

<span class="pl-s1">plt</span>.<span class="pl-en">clim</span>([<span class="pl-c1">-</span><span class="pl-c1">3</span>, <span class="pl-c1">-</span><span class="pl-c1">1</span>])  <span class="pl-c"># this breaks</span>
<span class="pl-c"># plt.clim([-3, 1])  # this works fine</span>
<span class="pl-c"># plt.clim([1, 3])  # this also breaks</span></pre></div>
<h3 dir="auto">Actual outcome</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/home/eytan/Documents/matplotlib_debug.py&quot;, line 11, in &lt;module&gt;
    plt.clim([-3, -1])  # this breaks
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/pyplot.py&quot;, line 2131, in clim
    im.set_clim(vmin, vmax)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cm.py&quot;, line 525, in set_clim
    self.norm.vmax = colors._sanitize_extrema(vmax)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colors.py&quot;, line 1161, in vmax
    self._changed()
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colors.py&quot;, line 1178, in _changed
    self.callbacks.process('changed')
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 292, in process
    self.exception_handler(exc)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 96, in _exception_printer
    raise exc
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 287, in process
    func(*args, **kwargs)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cm.py&quot;, line 618, in changed
    self.callbacks.process('changed', self)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 292, in process
    self.exception_handler(exc)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 96, in _exception_printer
    raise exc
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/cbook/__init__.py&quot;, line 287, in process
    func(*args, **kwargs)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py&quot;, line 551, in update_normal
    self.draw_all()
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py&quot;, line 608, in draw_all
    self._add_solids(X, Y, self._values[ind, np.newaxis])
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/colorbar.py&quot;, line 624, in _add_solids
    self.solids = self.ax.pcolormesh(
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/__init__.py&quot;, line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/axes/_axes.py&quot;, line 6058, in pcolormesh
    X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
  File &quot;/home/eytan/miniconda3/envs/mpl_debug/lib/python3.9/site-packages/matplotlib/axes/_axes.py&quot;, 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"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "/home/eytan/Documents/matplotlib_debug.py", line 11, in &lt;module&gt;
    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
</code></pre></div>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">A <code class="notranslate">pcolormesh</code> figure with a colorbar limit with a minimum of -3 and maximum of -1.</p>
<h3 dir="auto">Additional information</h3>
<p dir="auto">Works in version 3.4.3, breaks in version 3.5.0. Could it be related to the fix for <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1057493855" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/21671" data-hovercard-type="issue" data-hovercard-url="/matplotlib/matplotlib/issues/21671/hovercard" href="https://github.com/matplotlib/matplotlib/issues/21671">#21671</a> in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1058183168" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/21676" data-hovercard-type="pull_request" data-hovercard-url="/matplotlib/matplotlib/pull/21676/hovercard" href="https://github.com/matplotlib/matplotlib/pull/21676">#21676</a>? It seems like a similar issue and also showed up in version 3.5.0.</p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Ubuntu 20.04</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.5.2</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">TkAgg</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.9.12</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>