Reusing patch objects in matplotlib without moving them

I want to automatically generate a series of graphs that are tied to patches. If I try to reuse the patch object, it moves the position on the canvas.

This script (based on the answer to Yann's previous question) demonstrates what is happening.

import pylab as plt
import scipy as sp
import matplotlib.patches as patches

sp.random.seed(100)
x = sp.random.random(100)
y = sp.random.random(100)
patch = patches.Circle((.75,.75),radius=.25,fc='none')


def doplot(x,y,patch,count):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.scatter(x,y)
    ax.add_patch(patch)
    im.set_clip_path(patch)
    plt.savefig(str(count) + '.png')


for count in xrange(4):
    doplot(x,y,patch,count)

The first plot is as follows: Correct position of patch - first time plotted

But in the second "1.png" the patch has moved. Wrong position of the patch

However, reassembling does not move the patch. "2.png" and "3.png" look exactly like "1.png".

Can someone point me in the right direction of what I am doing wrong?

In fact, the corrections that I use are relatively complex and take some time to generate - I would prefer not to redo them every frame, if possible.

+5
2

, , ax.cla(), .

import pylab as plt
import scipy as sp
import matplotlib.patches as patches

sp.random.seed(100)
patch = patches.Circle((.75,.75),radius=.25,fc='none')

fig = plt.figure()
ax = fig.add_subplot(111)

def doplot(x,y,patch,count):
    ax.set_xlim(-0.2,1.2)
    ax.set_ylim(-0.2,1.2)
    x = sp.random.random(100)
    y = sp.random.random(100)
    im = ax.scatter(x,y)
    ax.add_patch(patch)
    im.set_clip_path(patch)
    plt.savefig(str(count) + '.png')
    ax.cla()

for count in xrange(4):
    doplot(x,y,patch,count)
+2

unutbu answer copy, . , add_patch, . axes, figure, extents, clip_box, transform window_extent . , , , . , extents, Bbox, , .

, , , . , , , , :

import copy 

def doplot(x,y,patch,count):
    newPatch = copy.copy(patch)
    fig = plt.figure(dpi=50)
    ax = fig.add_subplot(111)
    im = ax.scatter(x,y)
    ax.add_patch(newPatch)
    im.set_clip_path(newPatch)
    plt.savefig(str(count) + '.png')

fig.savefig(str(count) + '.png'). fig, , plt.savefig , , .

+2

All Articles