Problem with adding an artist from one axes instance to another

I try to take artists from one subplot instance and add them to
another:

···

-------------------------
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Patch, Rectangle
from matplotlib.lines import Line2D

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.add_patch(Rectangle((.5, 1.5), 1, .2))
fig2 = Figure()
canvas2 = FigureCanvas(fig2)
ax2 = fig2.add_subplot(111)
for artist in ax.get_child_artists():
    if isinstance(artist, Line2D):
        ax2.add_line(artist)
    elif isinstance(artist, Patch):
        ax2.add_patch(artist)

ax2.add_patch(Rectangle((1.5, 2.5), 1, .2, facecolor='r'))
ax2.set_aspect("equal")
ax2.autoscale_view()
w, h = fig2.get_size_inches()
xmin, xmax = ax2.get_xlim()
ymin, ymax = ax2.get_ylim()
xext = xmax - xmin
yext = ymax - ymin
if xext < yext:
    w = h * xext/yext
else:
    h = w * yext/xext
    
size = fig2.set_size_inches(w, h)
canvas2.print_figure('copy2.eps')
-------------------------

But this fails to plot the first rectange in the resulting plot. The
second, red rectangle is painted correctly in the resulting plot, but
the first one is totaly missing in the plot, leaving only a line in
the plot. Is there some kind of internal status that has to be
resettet in the actors?

Kind regards
Berthold
--
berthold@...1453... / <http://höllmanns.de/>
bhoel@...273... / <http://starship.python.net/crew/bhoel/>

But this fails to plot the first rectange in the resulting plot. The
second, red rectangle is painted correctly in the resulting plot, but
the first one is totaly missing in the plot, leaving only a line in
the plot. Is there some kind of internal status that has to be
resettet in the actors?

When you add an artist to the Axes, it checks to see if you have set a
transformation. If you haven't, it will set the default axes
transformation. If you have, it leaves the transformation unchanged.
This is why you are seeing the problems you see.

Before adding them to the second axes, you need to reset the
transformation for each line, text, etc....

for artist in ax.get_child_artists():
   artist.set_transform(ax2.transData)
   if isinstance(artist, Line2D):
       ax2.add_line(artist)
   elif ....

should work....

JDH