Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill matplotlib subplots by column, not row

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?

like image 403
jhin Avatar asked Mar 01 '18 16:03

jhin


1 Answers

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])
like image 132
DavidG Avatar answered Sep 22 '22 06:09

DavidG