0

Suppose I have two matplotlib plots, generated with code

import matplotlib.pyplot as plt

plt.figure()
set1a = np.array([[1, 1, 0.1], [2, 2, 0.1 ]])
set1b = np.array([[1, 2, 0.2], [2, 4, 0.2 ]])
plt.errorbar(x = set1a[:, 0], y = set1a[:, 1], yerr = set1a[:, 2], fmt='o', capsize=2, label='set1a')
plt.errorbar(x = set1b[:, 0], y = set1b[:, 1], yerr = set1b[:, 2], fmt='o', capsize=2, label='set1b')
plt.legend(loc='best', fontsize = 14)
plt.show()

plt.figure()
set2a = np.array([[0, 1, 0.1], [1, 2, 0.1 ]])
set2b = np.array([[0, 2, 0.2], [1, 4, 0.2 ]])
plt.errorbar(x = set2a[:, 0], y = set2a[:, 1], yerr = set2a[:, 2], fmt='o', capsize=2, label='set2a')
plt.errorbar(x = set2b[:, 0], y = set2b[:, 1], yerr = set2b[:, 2], fmt='o', capsize=2, label='set2b')
plt.legend(loc='best', fontsize = 14)
plt.show()

Using subplots I can create stacked plots

fig, (ax1, ax2) = plt.subplots(2)

With this, I could use directly ax1 and ax2 to plot the 2 panels, for example:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2)
set1a = np.array([[1, 1, 0.1], [2, 2, 0.1 ]])
set1b = np.array([[1, 2, 0.2], [2, 4, 0.2 ]])
ax1.errorbar(x = set1a[:, 0], y = set1a[:, 1], yerr = set1a[:, 2], fmt='o', capsize=2, label='set1a')
ax1.errorbar(x = set1b[:, 0], y = set1b[:, 1], yerr = set1b[:, 2], fmt='o', capsize=2, label='set1b')
ax1.legend(loc='best', fontsize = 14)

set2a = np.array([[0, 1, 0.1], [1, 2, 0.1 ]])
set2b = np.array([[0, 2, 0.2], [1, 4, 0.2 ]])
ax2.errorbar(x = set2a[:, 0], y = set2a[:, 1], yerr = set2a[:, 2], fmt='o', capsize=2, label='set2a')
ax2.errorbar(x = set2b[:, 0], y = set2b[:, 1], yerr = set2b[:, 2], fmt='o', capsize=2, label='set2b')
ax2.legend(loc='best', fontsize = 14)
fig.show()

I am interested, however, to obtain the same result as above, after I already created a plot like in the first code. Something like:

fig, (ax1, ax2) = plt.subplots(2)

plt.figure()
# ... code for a plot
# Here assign current plot to ax1

plt.figure()
# ... code for another plot
# Here assign current plot to ax2

Is this possible?

francesco
  • 5,823
  • 7
  • 19
  • 41

0 Answers0