<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">After upgrading to 3.0.0, colormaps added to images with mpl_toolkits.axes_grid1.make_axes_locatable().append_axes overwrite the entire image.  This happens in a jupyter notebook with %matplotlib inline but not with %matplotlib notebook.  Also sometimes seen in scripts saving to .PNG (but I cannot reproduce it today).</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<p dir="auto">This should be run as a Jupyter Notebook so see the problem.</p>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

a = np.random.normal(size=(300,300))
cmap = 'hot'

fig, ax = plt.subplots(figsize=(6,5))
imshow = ax.imshow(a,cmap=cmap)
divider = make_axes_locatable(ax)
cax = divider.append_axes(&quot;right&quot;, size=&quot;5%&quot;, pad=0.05)
plt.colorbar(imshow, cax=cax)"><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">from</span> <span class="pl-s1">mpl_toolkits</span>.<span class="pl-s1">axes_grid1</span> <span class="pl-k">import</span> <span class="pl-s1">make_axes_locatable</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-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span>(<span class="pl-c1">300</span>,<span class="pl-c1">300</span>))
<span class="pl-s1">cmap</span> <span class="pl-c1">=</span> <span class="pl-s">'hot'</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-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>,<span class="pl-c1">5</span>))
<span class="pl-s1">imshow</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">a</span>,<span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">cmap</span>)
<span class="pl-s1">divider</span> <span class="pl-c1">=</span> <span class="pl-en">make_axes_locatable</span>(<span class="pl-s1">ax</span>)
<span class="pl-s1">cax</span> <span class="pl-c1">=</span> <span class="pl-s1">divider</span>.<span class="pl-en">append_axes</span>(<span class="pl-s">"right"</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-s">"5%"</span>, <span class="pl-s1">pad</span><span class="pl-c1">=</span><span class="pl-c1">0.05</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">imshow</span>, <span class="pl-s1">cax</span><span class="pl-c1">=</span><span class="pl-s1">cax</span>)</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto">The colorbar overwrites the entire image.</p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">A nice colorbar next to the image - it worked in v. 2.1.1 and I am 99% sure it worked in later 2.X versions of matplotlib.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system:  MacOS 10.13.6  AND Ubuntu 18.04.1</li>
<li>Matplotlib version: 3.0.0</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>):  module://ipykernel.pylab.backend_inline</li>
<li>Python version: 3.7.0 and 3.6.6</li>
<li>Jupyter version (if applicable): notebook 5.7.0</li>
<li>Other libraries:</li>
</ul>
<h3 dir="auto">Problem</h3>
<p dir="auto">As discussed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1323308840" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/23525" data-hovercard-type="pull_request" data-hovercard-url="/matplotlib/matplotlib/pull/23525/hovercard" href="https://github.com/matplotlib/matplotlib/pull/23525">#23525</a>, the legend treats every label as distinct and will therefore display them even when visually indistinguishable.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1300499/185444858-4a539a36-7376-46ba-81be-6270ac235eca.png"><img src="https://user-images.githubusercontent.com/1300499/185444858-4a539a36-7376-46ba-81be-6270ac235eca.png" alt="image" style="max-width: 100%;"></a></p>
<h3 dir="auto">Proposed solution</h3>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/timhoffm/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/timhoffm">@timhoffm</a> raised the good point that we don't want to break the artist-(label, handle) link in case the artist gets updated, so I think an alternative solution is maybe to have this logic in the legend. Something like a <code class="notranslate">duplicate</code> keyword argument to legend:</p>
<ul dir="auto">
<li><code class="notranslate">duplicate=True</code>, default, current behavior</li>
<li><code class="notranslate">duplicate=False</code> - triggers a check of legend objects. If multiple legend handles have the same properties (class, color/edgecolor, hatch, size/width/edgewidth, etc) and the same label, then a new handle with those properties linked to all the artists is shown in place of the original handles.</li>
</ul>
<p dir="auto">I'm not sure if this is even doable in the current architecture, but if possible this would be designed so that updates to properties that update handles/legends propagate back to create new entries on the legend as needed. The advantage of this approach is that it would facilitate explicitly linking legend labels &amp; handles to multiple artists.</p>
<p dir="auto">I think currently duplicates are eliminated manually either by omitting the labels on some artists or creating a custom legend handle. Neither approach preserves a link between a legend entry and all the artists it is intended to describe.</p>