0
import matplotlib.pyplot as plt 
import numpy as np
n_rows = 2
n_cols = 1 
fig,axes = plt.subplots(n_rows,n_cols)
for row_num in range(n_rows):
    for col_num in range(n_cols):
        ax= axes [row_num][col_num]
        ax.plot(np.random.rand(20))
        ax.set_title(f'Plot {row_num+1}),{col_num+1})')
fig.suptitle('Main title')
fig.tight_layout()
plt.show()this should work perfectly clearly, but it gives an error

Axes' object is not subscriptable" Can you help me in finding answear? I expect to know why it does not work, and how I can answer that. Also if this could be caused by different python versions?

Witold
  • 1
  • 1
    `fig, (ax1, ax2) = plt.subplots(n_rows,n_cols)` is the easiest option when there are only 2 subplots. If there are more then use `fig, axes = plt.subplots(n_rows,n_cols)` with `axes = axes.flat` when then only requires a single loop. – Trenton McKinney Jun 06 '23 at 14:44
  • *...which, then only...* - Typo – Trenton McKinney Jun 06 '23 at 15:08

0 Answers0