3

I would like to use the axes handle returned by an external function to place the plot objects from that axes within my chosen subplot located in another figure. The format of the external function can't be modified. The below code is a dummy example that represents the structure of the code I am working with.

I am hoping there's an inbuilt function to do this such as set_figure() but I could not find one. Any help is appreciated.

import matplotlib.pyplot as plt

def myfunc():
    plt.figure()
    plt.text(0.5,0.5,s='plot from\nexternal function')
    return plt.gca()

plt.subplot(311)
plt.text(0.5,0.5,s='subplot 1')

plt.subplot(312)
plt.text(0.5,0.5,s='subplot 2')

plt.subplot(313)
ax = myfunc()
# how can I make the plot generated by myfunc appear in the third subplot
# and close the figure from the external function so it does not show?
plt.show()

The desired output looks like this:

desired output

Matthew Reid
  • 332
  • 2
  • 9
  • 2
    Well, matplotlib doesn't support this. The only thing that would work, is trying to convince the author of the function to allow to accept an `ax` parameter. – JohanC Mar 02 '21 at 11:10
  • See also: [copy an axes content and show it in a new figure](https://stackoverflow.com/questions/45810557/pyplot-copy-an-axes-content-and-show-it-in-a-new-figure) – JohanC Mar 02 '21 at 11:15

0 Answers0