I constantly generate an arbitrary number of auxiliary sections (sometimes the data leads to 3 sections, sometimes to 13, etc.). I wrote a small utility function to stop thinking about it.
The two functions that I define are as follows. You can change the stylistic choice to suit your preferences.
import math import numpy as np from matplotlib import pyplot as plt def choose_subplot_dimensions(k): if k < 4: return k, 1 elif k < 11: return math.ceil(k/2), 2 else:
And here is an example of use with 13 plots:
x_variable = list(range(-5, 6)) parameters = list(range(0, 13)) figure, axes = generate_subplots(len(parameters), row_wise=True) for parameter, ax in zip(parameters, axes): ax.plot(x_variable, [x**parameter for x in x_variable]) ax.set_title(label="y=x^{}".format(parameter)) plt.tight_layout() plt.show()
Which produces the following:

Or, switching to the column traversal order ( generate_subplots(..., row_wise=False) ) generates:

source share