简体   繁体   中英

How tuples are unpacked in matplotlib python while plotting subplots?

fig,((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = plt.subplots(3,3,sharex=True,sharey=True)

lineardata=np.array([1,2,3,4,5])

ax5.plot(lineardata,'-')

I have learnt that "plt.subplot" function returns a tuple,but i am not able to understand how they are unpacked in the fig, ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9

In order to explain how they are unpacked in the fig, ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9 , we start with a simple code:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)

Now we discover ax by using python console (python interpreter)

in[0]: ax
Out[0]: 
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)

it returns an array of dimension 3 x 3:

then, we share a snippet of your code

((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = ax

or

(ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9) = ax

in turn, we want to explore, how the contents of ax are distributed over the object names you gave.

This is done by exploring the relations between the left and right side of = in the later code- and again by using python console:

In[1]: ax1==ax[0][0]
Out[1]: True

and so on with others

In[2]: ax2==ax[0][1]
Out[2]: True
In[3]: ax3==ax[0][2]
Out[3]: True
In[4]: ax4==ax[1][0]
Out[4]: True
In[5]: ax5==ax[1][1]
Out[5]: True
In[6]: ax6==ax[1][2]
Out[6]: True
In[7]: ax7==ax[2][0]
Out[7]: True
In[8]: ax8==ax[2][1]
Out[8]: True
In[9]: ax9==ax[2][2]
Out[9]: True

so we deduced that the mapping has done as if

array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
=
array([[ax1, ax2, ax3],
       [ax4, ax5, ax6],
       [ax7, ax8, ax9]], dtype=object)

So the mapping relies on matching the sequence of the object you give with the sequences of objects in row one, row two until the last row.

we also deduce that mapping is done with the resulted array and is not a part of the subplots function itself.

finally, we should mention that the resultant array ax is a numpy.ndarray , even if you did not import the numpy it.

I hope the above illustration declare what you ask about

and waiting for your comment

matplotlib.pyplot.subplots returns a tuple with an figure-object and an array of axis-objects:

from matplotlib import pyplot as plt

fig,axs = plt.subplots()
axs[0].plot(...)

as the docs state:

Return values:

fig = Figure

ax = axes.Axes or array of Axes

If you have greater that 2x2 subplots, it may be easier to collect all the axes and the unpack them:

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

ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9 = axes.flatten()

ax1.plot(lineardata,'-')

or do it in a loop:

for ax in axes.flatten():
    ax.plot(lineardata,'-')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM