matplotlib-users

 [Matplotlib-users] Problem with adding an artist from one axes instance to another From: - 2007-02-07 20:46:06 ```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 =3D Figure() canvas =3D FigureCanvas(fig) ax =3D fig.add_subplot(111) ax.add_patch(Rectangle((.5, 1.5), 1, .2)) fig2 =3D Figure() canvas2 =3D FigureCanvas(fig2) ax2 =3D 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=3D'r')) ax2.set_aspect("equal") ax2.autoscale_view() w, h =3D fig2.get_size_inches() xmin, xmax =3D ax2.get_xlim() ymin, ymax =3D ax2.get_ylim() xext =3D xmax - xmin yext =3D ymax - ymin if xext < yext: w =3D h * xext/yext else: h =3D w * yext/xext =20=20=20=20 size =3D 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 --=20 berthold@... / ; bhoel@... / ; ```
 [Matplotlib-users] Fwd: Problem with adding an artist from one axes instance to another From: John Hunter - 2007-02-07 22:25:38 ```> 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 ```
 Re: [Matplotlib-users] Fwd: Problem with adding an artist from one axes instance to another From: - 2007-02-08 18:04:09 ```"John Hunter" writes: >> 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.... It does. It helps me a lot, thank you. Regards Berthold --=20 berthold@... / ; bhoel@... / ; ```