[Matplotlib-users] Axes array for subplots

Benjamin Root ben.v.root at gmail.com
Tue Apr 17 11:22:10 EDT 2018


Virgil,

How did you get from `axarr` to `ax`? The error message suggests that you
haven't fully indexed the result. Remember, with squeeze=False, `axarr`
will be a 2-D array, requiring two indices.

Ben

On Tue, Apr 17, 2018 at 11:14 AM, Virgil Stokes <vs at it.uu.se> wrote:

> Ok Ben,
>
> I tried the following per your suggestion:
>
>   fig, axarr = plt.subplots(NSub, figsize=(width,height), squeeze=False,
> sharex=True)
>
> but when the following is executed:
>
>   ax.grid(True)
>
> I get the following error message:
>   builtins.AttributeError: 'numpy.ndarray' object has no attribute 'grid'
>
> But, thanks for your help :-).
>
> On 2018-04-17 16:30, Benjamin Root wrote:
>
> By default, `squeeze` is called on the array prior to returning it. This
> way, users don't need to deal with 2D arrays when most of the time, they
> are dealing with 1D setups. You can specify squeeze=False to subplots to
> turn this behavior off and always have a 2D array.
>
> I hope that helps!
> Ben Root
>
>
> On Tue, Apr 17, 2018 at 10:12 AM, Virgil Stokes <vs at it.uu.se> wrote:
>
>> The following line is part of a much larger python (3.6) with Matplotlib
>> (2.2.2) program in which the number of subplots is determined from input
>> data:
>>
>>   fig, axarr = plt.subplots(NSub, figsize=(width,height), sharex=True)
>>
>> This works fine when the number of subplots (NSub) is greater than 1. For
>> example when NSub=3, axarr is an array of length 3 and contains:
>>
>>   array([<matplotlib.axes._subplots.AxesSubplot object at
>> 0x0000025900E9DDD8>,
>>          <matplotlib.axes._subplots.AxesSubplot object at
>> 0x00000259011897F0>,
>>          <matplotlib.axes._subplots.AxesSubplot object at
>> 0x00000259011C7128>],
>>         dtype=object)
>>
>> However, when NSub=1, axarr contains:
>>
>>   <matplotlib.axes._subplots.AxesSubplot object at 0x00000161AB26AE80>
>>
>> and of course, will give an error if axarr is an array; i.e.
>>
>>   builtins.TypeError: 'AxesSubplot' object does not support indexing
>>
>> For my code this requires special handling because axarr is no longer an
>> array. Why not have axarr contain:
>>
>>   array([<matplotlib.axes._subplots.AxesSubplot object at
>> 0x00000161AB26AE80>],dtype=object)
>>
>> when NSub=1. IMHO this is consistent; i.e. it is an array with a length
>> that is equal to the number of subplots.
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users at python.org
>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180417/e88b055b/attachment.html>


More information about the Matplotlib-users mailing list