nos nos - 4 months ago 27
Python Question

python matplotlib how to get multiple axes handles

My plot contains many subplots and I need a list of handles to treat each one of them. Right now I do the following

axes = [fig.add_subplot(2,3,1), fig.add_subplot(2,3,2),
fig.add_subplot(2,3,3), fig.add_subplot(2,3,4),
fig.add_subplot(2,3,5), fig.add_subplot(2,3,6)]


Is there a built-in function to get
axes
more succinctly by simply specifying 2, and 3?

Answer

You can use:

f, ((ax1, ax2, ax3),(ax4, ax5, ax6)) = plt.subplots(2, 3)

or:

f, axarray = plt.subplots(2, 3)