<Python, matplotlib> figure, axesの配列指定

matplotlibaxesを指定するとき、、

In [305]: import matplotlib.pyplot as plt

In [306]: f, ax = plt.subplots(nrows=2, ncols=2)



In [307]: ax
Out[307]: 
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000106C1E48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001911DB38>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000018E0B470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000001A676358>]], dtype=object)

In [308]: ax[0,0]
Out[308]: <matplotlib.axes._subplots.AxesSubplot at 0x106c1e48>

In [309]: f, ax = plt.subplots(nrows=1, ncols=1)



In [310]: ax
Out[310]: <matplotlib.axes._subplots.AxesSubplot at 0x18f1f080>

In [311]: ax[0,0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-311-5f90218d402c> in <module>()
----> 1 ax[0,0]

TypeError: 'AxesSubplot' object is not subscriptable

In [312]: f, ax = plt.subplots(nrows=2, ncols=1)



In [313]: ax
Out[313]: 
array([<matplotlib.axes._subplots.AxesSubplot object at 0x00000000136F66D8>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x0000000018C79160>], dtype=object)

In [314]: ax[0,0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-314-5f90218d402c> in <module>()
----> 1 ax[0,0]

IndexError: too many indices for array

In [315]: ax[0]
Out[315]: <matplotlib.axes._subplots.AxesSubplot at 0x136f66d8>

In [316]: 

という感じで、array[2,2]の時とarray[2,1]、array無の場合で、axのアクセス方法が異なる。。。 めんどくさい。。。

と、思ったんだが、、

なにやらマニュアルを読んだらsqueezeというオプションを指定すればいいらしい。。。

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots

f, ax = plt.subplots(nrows=1, ncols=1, squeeze=False)



In [330]: ax
Out[330]: array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000001827E048>]], dtype=object)

In [331]: f, ax = plt.subplots(nrows=1, ncols=1, squeeze=True)



In [332]: ax
Out[332]: <matplotlib.axes._subplots.AxesSubplot at 0x107f2c18>

In [333]: 

squeeze=Falseだとarrayで返ってきた。

なるほど。。。