Hello fellow programmers!
I am trying to write code that creates a matplotlib.pyplot.figure and fills it with subplots. The only input shall be the number of subplots. In this case every white box represents one subplot.
Following example works for subplot numbers that are divisible by 100:
import matplotlib.pyplot as plt
subplots = 200
cols = int(round(subplots/10))
rows = int(round(subplots/20))
print(f'subplots: {cols*rows}')
fig, axes = plt.subplots(nrows=rows, ncols=cols)
_axes = []
for ax_array in axes:
for ax in ax_array:
_axes.append(ax)
for ax in _axes:
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.tick_params(axis='both', which='both', length=0)
ax.plot()
plt.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Output:
subplots: 200
This figure is depicted correctly with 200 subplots.
But when I make the change subplots = 150, the amount of subplots is calculated and depicted wrong:
subplots: 120
How do I compute the correct amount of nrows and ncols required for depicting the subplots? I want the figure to be able to depict 149 subplots if required.
Is there a better way for doing this?
Thank you in advance.





