What does axes.flat do in matplotlib?

I have seen various programs using matplotlib that uses the axes.flat function, like this code:

for i, ax in enumerate(axes.flat):

what does it do?

+4
source share
1 answer

Let's look at a minimal example where we create several s axes plt.subplots, also see this question ,

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=2,nrows=3, sharex=True, sharey=True)

for i, ax in enumerate(axes.flat):
    ax.scatter([i//2+1, i],[i,i//3])

plt.show()

Here axesis an array with zero axes,

print(type(axes))
> <type 'numpy.ndarray'>
print(axes.shape)
> (3L, 2L)

axes.flatnot a function, this is an attribute numpy.ndarray:numpy.ndarray.flat

ndarray.flat1st iterator over the array.
This is an instance of numpy.flatiter, which acts similarly, but is not a subclass of the Pythons built-in iterator.

Example:

import numpy as np

a = np.array([[2,3],
              [4,5],
              [6,7]])

for i in a.flat:
    print(i)

which will print the numbers 2 3 4 5 6 7.

, 3x2,

for i, ax in enumerate(axes.flat):

, .

axes.flatten(), flatten() - numpy. :

for i, ax in enumerate(axes.flatten()):

. , , ( matplotlib).

flat1 = [ax for ax in axes.flat]
flat2 = axes.flatten()
print(flat1 == flat2)
> [ True  True  True  True  True  True]

, ,

for row in axes:
    for ax in row:
        ax.scatter(...)
+6

Source: https://habr.com/ru/post/1687934/


All Articles