Issue
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
Solution
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
Answered By - Nour-Allah Hussein
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.