1

Is it possible to copy the content of a subplot in another figure in matplotlib?

Something like this:

x = np.arange(10)
y = np.sin(x)

fig, ax = plt.subplots(1, 2, figsize=(10, 3))
ax[0].plot(x, y)
ax[1].plot(x, y, "o");


fig2, ax2 = plt.subplots(2,2)
ax2[0] = ax[0] ??

Edit:

A modified version of this question, works but still the figure has empty margins of the original figure

import matplotlib.pyplot as plt
import numpy as np
import pickle
import io

num_rows = 3
num_cols = 1
fig, axs = plt.subplots(num_rows, num_cols, sharex=True)
for i in range(num_rows):
     ax = axs[i]
     ax.plot(np.arange(10), np.arange(10)**i)

def func(index, fig):

    buf = io.BytesIO()
    pickle.dump(fig, buf)
    buf.seek(0)
    fig2 = pickle.load(buf) 

    for i, ax in enumerate(fig2.axes):
        if i != index:
            fig2.delaxes(ax)
        else:
            axes=ax

    axes.change_geometry(1,1,1)


func(0, fig)
plt.close(fig)
plt.show()

Is it possible to crop the extra space?

Abolfazl
  • 1,047
  • 2
  • 12
  • 29

0 Answers0