Large number of subplots with matplotlib

kiriloff picture kiriloff · Nov 13, 2012 · Viewed 26.6k times · Source

I would like to create plot with many (100) subplots with Python matplotlib. I cannot find appropriate syntax for it:

I would like something like (this is not working)

plt.subplot(10,10,i,X1, Y) 

in a loop with i from 0 to 99, then

plt.show()

Syntax is available in many tutorials for case when there are only few subplots. Then, syntax can be

plt.close('all')
fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)

plt.tight_layout()

code is from here.

For my problem, I guess that I cannot use the same syntax, as I would have plt.subplot(10101), etc., which I don't understand.

Do you have a solution?

Thanks

Answer

jorgeca picture jorgeca · Nov 13, 2012

Try this:

fig, ax = plt.subplots(10, 10)

where ax will contain one hundred axis in a list (of lists).

It is a really handy function, from the docs:

Definition: plt.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, **fig_kw)
Create a figure with a set of subplots already made.

This utility wrapper makes it convenient to create common layouts of
subplots, including the enclosing figure object, in a single call.