<p dir="auto">So, I was reading the online documentation of MPL on colormaps, and noticed that there is an error in it.<br>
In the section of <a href="https://matplotlib.org/3.1.1/tutorials/colors/colormap-manipulation.html#creating-listed-colormaps" rel="nofollow">"Creating listed colormaps"</a>, it mentions that the dynamic range of a colormap can be reduced by using:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="viridisBig = cm.get_cmap('viridis', 512)
newcmp = ListedColormap(viridisBig(np.linspace(0.25, 0.75, 256)))
plot_examples([viridis, newcmp])"><pre class="notranslate"><span class="pl-s1">viridisBig</span> <span class="pl-c1">=</span> <span class="pl-s1">cm</span>.<span class="pl-en">get_cmap</span>(<span class="pl-s">'viridis'</span>, <span class="pl-c1">512</span>)
<span class="pl-s1">newcmp</span> <span class="pl-c1">=</span> <span class="pl-v">ListedColormap</span>(<span class="pl-en">viridisBig</span>(<span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.25</span>, <span class="pl-c1">0.75</span>, <span class="pl-c1">256</span>)))
<span class="pl-en">plot_examples</span>([<span class="pl-s1">viridis</span>, <span class="pl-s1">newcmp</span>])</pre></div>
<p dir="auto">It also specifically mentions that this must be done in order to make sure that the new colormap does not have repeated values.</p>
<p dir="auto">However, as <code class="notranslate">viridis</code> is a <code class="notranslate">ListedColormap</code> and <code class="notranslate">ListedColormap</code> objects do not allow for interpolation between values, both <code class="notranslate">viridisBig</code> and <code class="notranslate">newcmp</code> will have repeated values, despite the documentation specifically mentioning they do not.</p>
<p dir="auto">This could be fixed by either changing the tutorial into:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="viridisRGB = viridis(np.linspace(0, 1, viridis.N))
viridisBig = LinearSegmentedColormap.from_list('viridisBig', viridisRGB, 512)
newcmp = ListedColormap(viridisBig(np.linspace(0.25, 0.75, 256)))
plot_examples([viridis, newcmp])"><pre class="notranslate"><span class="pl-s1">viridisRGB</span> <span class="pl-c1">=</span> <span class="pl-en">viridis</span>(<span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-s1">viridis</span>.<span class="pl-v">N</span>))
<span class="pl-s1">viridisBig</span> <span class="pl-c1">=</span> <span class="pl-v">LinearSegmentedColormap</span>.<span class="pl-en">from_list</span>(<span class="pl-s">'viridisBig'</span>, <span class="pl-s1">viridisRGB</span>, <span class="pl-c1">512</span>)
<span class="pl-s1">newcmp</span> <span class="pl-c1">=</span> <span class="pl-v">ListedColormap</span>(<span class="pl-en">viridisBig</span>(<span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.25</span>, <span class="pl-c1">0.75</span>, <span class="pl-c1">256</span>)))
<span class="pl-en">plot_examples</span>([<span class="pl-s1">viridis</span>, <span class="pl-s1">newcmp</span>])</pre></div>
<p dir="auto">as <code class="notranslate">LinearSegmentedColormap</code> objects do allow for interpolation when creating them, or obviously by making <code class="notranslate">ListedColormap</code> objects be able to perform interpolation.</p>
<p dir="auto">I have however noticed that the interpolation messes up sometimes, especially at the ends of the colormap.<br>
It might therefore be better to simply take the required part of the colormap when reducing the dynamic range, with something like:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="newcmp = ListedColormap(viridis(np.linspace(0.25, 0.75, viridis.N*(0.75-0.25))))
plot_examples([viridis, newcmp])"><pre class="notranslate"><span class="pl-s1">newcmp</span> <span class="pl-c1">=</span> <span class="pl-v">ListedColormap</span>(<span class="pl-en">viridis</span>(<span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.25</span>, <span class="pl-c1">0.75</span>, <span class="pl-s1">viridis</span>.<span class="pl-v">N</span><span class="pl-c1">*</span>(<span class="pl-c1">0.75</span><span class="pl-c1">-</span><span class="pl-c1">0.25</span>))))
<span class="pl-en">plot_examples</span>([<span class="pl-s1">viridis</span>, <span class="pl-s1">newcmp</span>])</pre></div>
<p dir="auto">This obviously also reduces the resolution of the colormap, so it is not a good solution, but works well enough on colormaps with 256 segments and a not too big reduction in dynamic range.</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">I am updating a line plot. There is an event trigger that starts this updating. If the trigger came from the figure that contains the plot, everything is fine. However, if the trigger came from another figure, then weird results happen: the line that's been updated appears to leave its trace uncleared.</p>
<p dir="auto"><strong>Code for reproduction</strong></p>

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

def onclick(event):

    for ii in np.linspace(0., np.pi, 100):
        y1 = y * np.sin(ii)
        line1.set_ydata(y1)
        ax.draw_artist(line1)
        line2.set_ydata(-y1)
        ax2.draw_artist(line2)
        ax2.set_ylim(y1.min(), y1.max())
        fig.canvas.update()
        plt.pause(0.1)


x = np.linspace(0., 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure()
ax = fig.add_subplot(1, 2, 1)
line1 = ax.plot(x, y)[0]

ax2 = fig.add_subplot(1, 2, 2)
line2 = ax2.plot(x, y)[0]


fig2 = plt.figure()

cid = fig2.canvas.mpl_connect('button_press_event', onclick)
# cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()"><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">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-k">def</span> <span class="pl-en">onclick</span>(<span class="pl-s1">event</span>):

    <span class="pl-k">for</span> <span class="pl-s1">ii</span> <span class="pl-c1">in</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.</span>, <span class="pl-s1">np</span>.<span class="pl-s1">pi</span>, <span class="pl-c1">100</span>):
        <span class="pl-s1">y1</span> <span class="pl-c1">=</span> <span class="pl-s1">y</span> <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-en">sin</span>(<span class="pl-s1">ii</span>)
        <span class="pl-s1">line1</span>.<span class="pl-en">set_ydata</span>(<span class="pl-s1">y1</span>)
        <span class="pl-s1">ax</span>.<span class="pl-en">draw_artist</span>(<span class="pl-s1">line1</span>)
        <span class="pl-s1">line2</span>.<span class="pl-en">set_ydata</span>(<span class="pl-c1">-</span><span class="pl-s1">y1</span>)
        <span class="pl-s1">ax2</span>.<span class="pl-en">draw_artist</span>(<span class="pl-s1">line2</span>)
        <span class="pl-s1">ax2</span>.<span class="pl-en">set_ylim</span>(<span class="pl-s1">y1</span>.<span class="pl-en">min</span>(), <span class="pl-s1">y1</span>.<span class="pl-en">max</span>())
        <span class="pl-s1">fig</span>.<span class="pl-s1">canvas</span>.<span class="pl-en">update</span>()
        <span class="pl-s1">plt</span>.<span class="pl-en">pause</span>(<span class="pl-c1">0.1</span>)


<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0.</span>, <span class="pl-c1">2</span><span class="pl-c1">*</span><span class="pl-s1">np</span>.<span class="pl-s1">pi</span>, <span class="pl-c1">100</span>)
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">sin</span>(<span class="pl-s1">x</span>)

<span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>()
<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">1</span>)
<span class="pl-s1">line1</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">plot</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>)[<span class="pl-c1">0</span>]

<span class="pl-s1">ax2</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>)
<span class="pl-s1">line2</span> <span class="pl-c1">=</span> <span class="pl-s1">ax2</span>.<span class="pl-en">plot</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>)[<span class="pl-c1">0</span>]


<span class="pl-s1">fig2</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>()

<span class="pl-s1">cid</span> <span class="pl-c1">=</span> <span class="pl-s1">fig2</span>.<span class="pl-s1">canvas</span>.<span class="pl-en">mpl_connect</span>(<span class="pl-s">'button_press_event'</span>, <span class="pl-s1">onclick</span>)
<span class="pl-c"># cid = fig.canvas.mpl_connect('button_press_event', onclick)</span>

<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>

<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/25810085/34643295-d30d6034-f2d6-11e7-80ee-0855ad34a981.png"><img src="https://user-images.githubusercontent.com/25810085/34643295-d30d6034-f2d6-11e7-80ee-0855ad34a981.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Expected outcome</strong></p>


<p dir="auto">Not the many curves saw in the above figure, just one curve being updated in each cycle. In the code snippet, if use</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cid = fig.canvas.mpl_connect('button_press_event', onclick)"><pre class="notranslate"><code class="notranslate">cid = fig.canvas.mpl_connect('button_press_event', onclick)
</code></pre></div>
<p dir="auto">then it works as intended.</p>
<p dir="auto">Also note, if you resize the plot, or save it as figure, then all the residue curves will be gone.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: windows</li>
<li>Matplotlib version: 2.0.0</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>): Qt4Agg</li>
<li>Python version: 2.7</li>
<li>Jupyter version (if applicable):</li>
<li>Other libraries:</li>
</ul>