2

Assume a library function that creates a figure internally:

def plot_data(data):  # cannot change this implementation
   ...
   fig, ax = plt.subplots(1, figsize=(w, h))
   ax.plot(...)
   ...
   return fig, ax

I create two plots using the above function:

fig1, ax1 = plot_data(data1)
fig2, ax2 = plot_data(data2)

How can I put these two plots/figures side by side? A naive approach is as follows (does not work this way):

fig0, ax0 = plt.subplots(1, 2, figsize=(w, h))
ax0[0] = ax1
ax0[1] = ax2
plt.close(fig1)
plt.close(fig2)
plt.show()
user1128016
  • 1,438
  • 3
  • 16
  • 17
  • Did you check [this](https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.pyplot.subplots.html) – rpanai Aug 09 '20 at 13:55
  • Possible duplicate of https://stackoverflow.com/questions/42818361/how-to-make-two-plots-side-by-side-using-python – ranaalisaeed Aug 09 '20 at 14:31
  • subplots creates new axes, the question is how to combine existing ones – user1128016 Aug 09 '20 at 14:45
  • Thats not really possible. Preferable is that the function does not create the figure or axes, but rather acts on an existing axes. – Jody Klymak Aug 09 '20 at 15:21
  • You might interested in [this](https://stackoverflow.com/a/46906599/10315163), but why bother that. Just change `plot_data(data)` to `plot_data(ax, data)` would make life much more easier. – Ynjxsjmh Aug 09 '20 at 16:07
  • plot_data is a library function that cannot be changed @Ynjxsjmh – user1128016 Nov 01 '20 at 15:28

0 Answers0