By default, matplotlib subplots are filled by row, not by column. To clarify, the commands
plt.subplot(nrows=3, ncols=2, idx=2)
plt.subplot(nrows=3, ncols=2, idx=3)
first plot into the upper right plot of the 3x2 plot grid (idx=2), and then into the middle left plot (idx=3).
Sometimes it may be desirable for whatever reason to fill the subplots by row, not by column (for example, because directly consecutive plots belong together and are easier interpretable when positioned below each other, rather than next to each other). How can this be achieved?
You can create the 3x2 array of axes using:
fig, axes = plt.subplots(nrows=3, ncols=2)
If you transpose this array, then flatten you can plot column wise, rather than row wise:
fig, axes = plt.subplots(nrows=3, ncols=2)
for ax in axes.T.flatten():
ax.plot([1,2,3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With