0

Using this thread I attempted to load a figure from a pickle file, pull out its axis (which contains the plot I'm speculating?) and plot the axis on an existing figure as a subplot.

fig = plt.figure(figsize=(16,12))
gs = fig.add_gridspec(4,2)

amb = pickle.load(open('/path/to/pickle.pkl', 'rb'))
ax_amb = amb.axes[0]
ax_amb.remove()
ax_amb.figure=fig
ax5 = fig.add_subplot(gs[3, 1]) 
ax_amb.set_position(ax5.get_position())
ax_amb.set_subplotspec(gs[3,1])
fig.axes.append(ax_amb)

The problem is that it shows a blank axis in the [3,1] position when I run plt.show(), and nothing is plotted.

NaN
  • 643
  • 1
  • 8
  • 21
  • The code you quote uses `fig2.add_axes(ax)`, which is missing here. – ImportanceOfBeingErnest Mar 22 '20 at 21:06
  • @ImportanceOfBeingErnest Do I need to make a `fig2`, add the pickle file axes to that `fig2` first, then add my other axes to `fig2`? – NaN Mar 22 '20 at 21:11
  • Note that the whole thing is a workaround anyways. So it will sure cause some trouble at some point. But to make your code work, use `.add_axes` instead of `axes.append`. – ImportanceOfBeingErnest Mar 22 '20 at 23:01
  • @ImportanceOfBeingErnest Ok, I was able to get it to plot on the same figure, but it's not putting the axis where I told it to go: `fig.add_subplot(gs[3, 1])`. Do you think it is because I'm using gridspec? it seems it is not scaling correctly. – NaN Mar 22 '20 at 23:20
  • Yes, the transforms are still the ones from the old figure and both figures differ significantly in size. Moving an axes from one figure to another is simply not a supported feature in matplotlib, so it will fail at one point or another. – ImportanceOfBeingErnest Mar 23 '20 at 11:55
  • @ImportanceOfBeingErnest Gotcha, would be a really nice feature, especially if importing from a separate `pickle` file. I'll try to simply put an image from a `png` file of the plot on where the axis would have been and see if it doesn't look too bad. – NaN Mar 25 '20 at 21:33
  • Just plot everything to the new figure. I.e. copy the code from the script that produced the pickle file, insert it into your current script and hence produce exactly the figure you want. – ImportanceOfBeingErnest Mar 25 '20 at 21:36

0 Answers0