绘制子图时如何在 matplotlib python 中解压缩元组?
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,'-')
我知道“plt.subplot”函数 returns 一个元组,但我无法理解它们在图中 ax1、ax2、ax3 中是如何解包的,ax4,ax5,ax6,ax7,ax8,ax9
matplotlib.pyplot.subplots
returns 一个带有图形对象和轴对象数组的元组:
from matplotlib import pyplot as plt
fig,axs = plt.subplots()
axs[0].plot(...)
作为 docs 状态:
Return values:
fig = Figure
ax = axes.Axes or array of Axes
为了解释how they are unpacked in the fig, ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9
,我们先来一段简单的代码:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
现在我们通过使用 python 控制台(python 解释器)
发现 ax
in[0]: ax
Out[0]:
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
它 returns 一个维度为 3 x 3 的数组:
然后,我们分享您的代码片段
((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = ax
或
(ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9) = ax
反过来,我们想探索 ax 的内容是如何分布在您提供的对象名称上的。
这是通过在后面的代码中探索 =
的左右两边之间的关系来完成的——并再次使用 python console:
In[1]: ax1==ax[0][0]
Out[1]: True
以此类推
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
所以我们推断映射已经完成,就好像
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
=
array([[ax1, ax2, ax3],
[ax4, ax5, ax6],
[ax7, ax8, ax9]], dtype=object)
因此映射依赖于将您提供的对象序列与第一行、第二行直到最后一行中的对象序列相匹配。
我们还推断映射是用结果数组完成的,而不是 subplots
函数本身的一部分。
最后,我们应该提到结果数组 ax
是一个 numpy.ndarray
,即使您没有导入 numpy
它。
希望上面的图说明你问的问题
等待您的评论
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,'-')
我知道“plt.subplot”函数 returns 一个元组,但我无法理解它们在图中 ax1、ax2、ax3 中是如何解包的,ax4,ax5,ax6,ax7,ax8,ax9
matplotlib.pyplot.subplots
returns 一个带有图形对象和轴对象数组的元组:
from matplotlib import pyplot as plt
fig,axs = plt.subplots()
axs[0].plot(...)
作为 docs 状态:
Return values:
fig = Figure
ax = axes.Axes or array of Axes
为了解释how they are unpacked in the fig, ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9
,我们先来一段简单的代码:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
现在我们通过使用 python 控制台(python 解释器)
发现ax
in[0]: ax
Out[0]:
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
它 returns 一个维度为 3 x 3 的数组:
然后,我们分享您的代码片段
((ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9)) = ax
或
(ax1,ax2,ax3),(ax4,ax5,ax6),(ax7,ax8,ax9) = ax
反过来,我们想探索 ax 的内容是如何分布在您提供的对象名称上的。
这是通过在后面的代码中探索 =
的左右两边之间的关系来完成的——并再次使用 python console:
In[1]: ax1==ax[0][0]
Out[1]: True
以此类推
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
所以我们推断映射已经完成,就好像
array([[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>]], dtype=object)
=
array([[ax1, ax2, ax3],
[ax4, ax5, ax6],
[ax7, ax8, ax9]], dtype=object)
因此映射依赖于将您提供的对象序列与第一行、第二行直到最后一行中的对象序列相匹配。
我们还推断映射是用结果数组完成的,而不是 subplots
函数本身的一部分。
最后,我们应该提到结果数组 ax
是一个 numpy.ndarray
,即使您没有导入 numpy
它。
希望上面的图说明你问的问题
等待您的评论