Move patch rather than remove it
<p>I have a graph that is gradually revealed. Since this should happen with a huge dataset and on several subplots simultaneously, I was planning to <em>move</em> the <code>patch</code> rather than remove it and draw it from scratch in order to accelerate the code.</p>

<p>My problem is similar to <a href="http://stackoverflow.com/questions/16527930/matplotlib-update-position-of-patches-or-set-xy-for-circles">this question</a>. However, I could not resolve it.</p>

<p>Here is a <em>minimal</em> working example:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

nmax = 10
xdata = range(nmax)
ydata = np.random.random(nmax)

fig, ax = plt.subplots()
ax.plot(xdata, ydata, 'o-')
ax.xaxis.set_ticks(xdata)
plt.ion()

i = 0
while i &lt;= nmax:
    # ------------ Here I would like to move it rather than remove and redraw.
    if i &gt; 0:
        rect.remove()
    rect = plt.Rectangle((i, 0), nmax - i, 1, zorder = 10)
    ax.add_patch(rect)
    # ------------
    plt.pause(0.1)
    i += 1

plt.pause(3)
</code></pre>

<p>Maybe this works for you.  Instead of removing the patch, you just update its position and its width (with <code>rect.set_x(left_x)</code> and <code>rect.set_width(width)</code>), and then redraw the canvas.  Try replacing your loop with this (note that the <code>Rectangle</code> is created <em>once</em>, before the loop):</p>

<pre><code>rect = plt.Rectangle((0, 0), nmax, 1, zorder=10)
ax.add_patch(rect)

for i in range(nmax):
    rect.set_x(i)
    rect.set_width(nmax - i)
    fig.canvas.draw()
    plt.pause(0.1)
</code></pre>

python matplotlib