If you create a 2D array of plots, e.g. with:
>>> fig, axarray = plt.subplots(3, 4)
then axarray
is a 2D array of objects, with each element containing a matplotlib.axes.AxesSubplot
:
>>> axarray.shape
(3, 4)
The problem is that when you index axarray[0]
, you're actually indexing a whole row of that array, containing several axes:
>>> axarray[0].shape
(4,)
>>> type(axarray[0])
numpy.ndarray
However, if you address a single element in the array then you can set its attributes in the normal way:
>>> type(axarray[0,0])
matplotlib.axes.AxesSubplot
>>> axarray[0,0].set_title('Top left')
A quick way of setting the attributes of all of the axes in the array is to loop over a flat iterator on the axis array:
for ii,ax in enumerate(axarray.flat):
ax.set_title('Axis %i' %ii)
Another thing you can do is 'unpack' the axes in the array into a nested tuple of individual axis objects, although this gets a bit awkward when you're dealing with large numbers of rows/columns:
fig, ((ax1, ax2, ax3, ax4), (ax5, ax6, ax7, ax8), (ax9, ax10, ax11, ax12)) \
= plt.subplots(3,4)