# [Matplotlib] subplot 理解

#### 正文

fig, ax = plt.subplots()


plt.subplots() 返回一个 Figure实例fig 和一个 AxesSubplot实例ax 。这个很好理解，fig代表整个图像，ax代表坐标轴和画的图。

fig, ax = plt.subplots(2,3)


print (ax)
#输出：
[[<matplotlib.axes._subplots.AxesSubplot object at 0x000000CED4AEB5C0>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD469630>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD27C898>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x000000CED4779CF8>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD4477B8>
<matplotlib.axes._subplots.AxesSubplot object at 0x000000CECD1BC748>]]


print(ax[0][0])
print(ax[0][1])
print(ax[0][2])
print(ax[1][0])
print(ax[1][1])
print(ax[1][2])
#输出:
Axes(0.125,0.536818;0.227941x0.343182)
Axes(0.398529,0.536818;0.227941x0.343182)
Axes(0.672059,0.536818;0.227941x0.343182)
Axes(0.125,0.125;0.227941x0.343182)
Axes(0.398529,0.125;0.227941x0.343182)
Axes(0.672059,0.125;0.227941x0.343182)


import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig1 = plt.figure()
patches.Rectangle(
(0.125,0.536818),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

patches.Rectangle(
(0.398529,0.536818),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

patches.Rectangle(
(0.672059,0.536818),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

patches.Rectangle(
(0.125,0.125),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

patches.Rectangle(
(0.398529,0.125),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

patches.Rectangle(
(0.672059,0.125),   # (x,y)
0.227941,          # width
0.343182,          # height
)
)

# fig1.savefig('rect1.png', dpi=90, bbox_inches='tight')
plt.show()


[1]: https://matplotlib.org/gallery/lines_bars_and_markers/simple_plot.html

03-18 2万+

01-16 2万+
07-24 1万+
05-25 11万+
09-30 2万+
01-01 431
08-17 6938
03-24 4792
06-27 1733
04-27 7万+
03-10 1049
03-28 2024