Reputation: 10912
With a two dimensional array "axes" when running this code:
print(axes.shape)
print(axes)
for ax in np.nditer(axes, flags=["refs_ok"]):
print(type(ax))
Produces this output:
(10, 4)
[[<matplotlib.axes._subplots.AxesSubplot object at 0x1a215d8908>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1dcd37b8>
<matplotlib.axes._subplots.AxesSubplot object at 0x115e3ad30>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1df18b00>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x115e84f60>
<matplotlib.axes._subplots.AxesSubplot object at 0x115e84780>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e163f60>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a23a75208>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a23a54860>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1b5ffb00>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a239a5550>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e4bd6a0>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a20238be0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1d0becf8>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a21ce1d30>
<matplotlib.axes._subplots.AxesSubplot object at 0x1165de6a0>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a1d2feb70>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1ceee320>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a2000d9e8>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e2fe278>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a1b6f8da0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1d361dd8>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a21dd0470>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e6f93c8>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a206b3320>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a20568588>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1b5d79b0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1cadebe0>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e536208>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1b173cc0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a21a09f60>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e58f588>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a221ee048>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a21b76e80>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1f6209b0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a21b54d68>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x1a1fbb6358>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1b52fe80>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a1e5108d0>
<matplotlib.axes._subplots.AxesSubplot object at 0x1a2034fb38>]]
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
It looks to me like first level of the array contains 10 subarrays or rows, and each of those contain four AxesSubPlot objects. How come then, does the output of the for loop say that they are ndarrays?
Upvotes: 1
Views: 626
Reputation: 282026
In your nditer
loop, you're not iterating over the elements of the array. You're iterating over 0-dimensional subarrays, each one a read-only view of a single cell of axes
. The type
you're seeing is the type of the 0-dimensional subarray, not the actual array contents.
If you want to iterate over the objects in an N-dimensional array of object
dtype, the simplest way is to iterate over axes.flat
:
for ax in axes.flat:
do_whatever_with(ax)
For arrays of other dtypes, it's usually best to avoid explicit iteration.
Upvotes: 3