3

I have a 4x4 numpy array, ax, where each element is an <class 'matplotlib.axes._subplots.AxesSubplot'> object.

This is how ax looks

>>> ax
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x124366f98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1246d0160>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124392710>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x125c0cdd8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1270032b0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x127124908>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12afcaf98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x127176668>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x127170cf8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124b1e3c8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1270c2a58>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124aeb128>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x124b0d7b8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12af52e48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x124a96518>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x12910cba8>]],
      dtype=object)

I need to plot each of these as a subplot in a four by four plot. This is what I am trying to get (ignore the dimensions of this plot)enter image description here

I tried the simple plt.show(ax) but I obviously ran into errors since ax is a collection of AxesSubplot class. I also tried looping through the ax array and plotting but I got the same error as I got when I ran plt.show(ax). This is the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 253, in show
    return _show(*args, **kw)
  File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 207, in show
    if block:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How do I plot a collection of AxesSubplot classes?

Clock Slave
  • 7,627
  • 15
  • 68
  • 109
  • `plt.show()` does not take any arguments, other than `True` or `False`. I do not understand the rest of the issue. Your axes array is part of a figure; so you need to show that figure. – ImportanceOfBeingErnest Aug 30 '19 at 08:39
  • I just realized the error I was making. I was returning only the axes part from the function. I changed that to return the figure as well. I call the `.show()` method on it and it works. This post looks like the product of a brain fade now. Thanks for your input. – Clock Slave Aug 30 '19 at 09:06

1 Answers1

0

you must loop through the axesSubplot instances and call fig.axes.append(ax)

  • I found this as reference: https://stackoverflow.com/questions/6309472/matplotlib-can-i-create-axessubplot-objects-then-add-them-to-a-figure-instance/46906599 – Joseph Holland Aug 30 '19 at 07:05