Skip to content

Commit dab8b6f

Browse files
committed
3.3
1 parent e928318 commit dab8b6f

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

3.3.md

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# 使用 GridSpec 自定义子图位置
2+
3+
> 原文:[Customizing Location of Subplot Using GridSpec](http://matplotlib.org/users/gridspec.html)
4+
5+
> 译者:[飞龙](https://github.com/)
6+
7+
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
8+
9+
`GridSpec`
10+
11+
指定子图将放置的网格的几何位置。 需要设置网格的行数和列数。 子图布局参数(例如,左,右等)可以选择性调整。
12+
13+
`SubplotSpec`
14+
15+
指定在给定`GridSpec`中的子图位置。
16+
17+
`subplot2grid`
18+
19+
一个辅助函数,类似于`pyplot.subplot`,但是使用基于 0 的索引,并可使子图跨越多个格子。
20+
21+
## `subplot2grid`基本示例
22+
23+
要使用subplot2grid,你需要提供网格的几何形状和网格中子图的位置。 对于简单的单网格子图:
24+
25+
```py
26+
ax = plt.subplot2grid((2,2),(0, 0))
27+
```
28+
29+
等价于:
30+
31+
```
32+
ax = plt.subplot(2,2,1)
33+
```
34+
35+
```
36+
nRow=2, nCol=2
37+
(0,0) +-------+-------+
38+
| 1 | |
39+
+-------+-------+
40+
| | |
41+
+-------+-------+
42+
```
43+
44+
要注意不想`subplot``gridspec`中的下标从 0 开始。
45+
46+
为了创建跨越多个格子的子图,
47+
48+
```py
49+
ax2 = plt.subplot2grid((3,3), (1, 0), colspan=2)
50+
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
51+
```
52+
53+
例如,下列命令:
54+
55+
```py
56+
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
57+
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
58+
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
59+
ax4 = plt.subplot2grid((3,3), (2, 0))
60+
ax5 = plt.subplot2grid((3,3), (2, 1))
61+
```
62+
63+
会创建:
64+
65+
![](http://matplotlib.org/_images/demo_gridspec01.png)
66+
67+
## `GridSpec``SubplotSpec`
68+
69+
你可以显式创建`GridSpec `并用它们创建子图。
70+
71+
例如,
72+
73+
```py
74+
ax = plt.subplot2grid((2,2),(0, 0))
75+
```
76+
77+
等价于:
78+
79+
```py
80+
import matplotlib.gridspec as gridspec
81+
gs = gridspec.GridSpec(2, 2)
82+
ax = plt.subplot(gs[0, 0])
83+
```
84+
85+
`gridspec `示例提供类似数组(一维或二维)的索引,并返回`SubplotSpec`实例。例如,使用切片来返回跨越多个格子的`SubplotSpec`实例。
86+
87+
上面的例子会变成:
88+
89+
```py
90+
gs = gridspec.GridSpec(3, 3)
91+
ax1 = plt.subplot(gs[0, :])
92+
ax2 = plt.subplot(gs[1,:-1])
93+
ax3 = plt.subplot(gs[1:, -1])
94+
ax4 = plt.subplot(gs[-1,0])
95+
ax5 = plt.subplot(gs[-1,-2])
96+
```
97+
98+
![](http://matplotlib.org/_images/demo_gridspec02.png)
99+
100+
## 调整 `GridSpec`布局
101+
102+
在显式使用`GridSpec`的时候,你可以调整子图的布局参数,子图由`gridspec`创建。
103+
104+
```py
105+
gs1 = gridspec.GridSpec(3, 3)
106+
gs1.update(left=0.05, right=0.48, wspace=0.05)
107+
```
108+
109+
这类似于`subplots_adjust`,但是他只影响从给定`GridSpec`创建的子图。
110+
111+
下面的代码
112+
113+
```py
114+
gs1 = gridspec.GridSpec(3, 3)
115+
gs1.update(left=0.05, right=0.48, wspace=0.05)
116+
ax1 = plt.subplot(gs1[:-1, :])
117+
ax2 = plt.subplot(gs1[-1, :-1])
118+
ax3 = plt.subplot(gs1[-1, -1])
119+
120+
gs2 = gridspec.GridSpec(3, 3)
121+
gs2.update(left=0.55, right=0.98, hspace=0.05)
122+
ax4 = plt.subplot(gs2[:, :-1])
123+
ax5 = plt.subplot(gs2[:-1, -1])
124+
ax6 = plt.subplot(gs2[-1, -1])
125+
```
126+
127+
会产生
128+
129+
![](http://matplotlib.org/_images/demo_gridspec03.png)
130+
131+
## 使用 `SubplotSpec`创建 `GridSpec`
132+
133+
你可以从`SubplotSpec`创建`GridSpec`,其中它的布局参数设置为给定`SubplotSpec`的位置的布局参数。
134+
135+
```py
136+
gs0 = gridspec.GridSpec(1, 2)
137+
138+
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
139+
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
140+
```
141+
142+
![](http://matplotlib.org/_images/demo_gridspec04.png)
143+
144+
## 使用`SubplotSpec`创建复杂嵌套的`GridSpec`
145+
146+
这里有一个更复杂的嵌套`gridspec`的示例,我们通过在每个 3x3 内部网格中隐藏适当的脊线,在 4x4 外部网格的每个单元格周围放置一个框。
147+
148+
![](http://matplotlib.org/_images/demo_gridspec06.png)
149+
150+
## 网格尺寸可变的`GridSpec`
151+
152+
通常,`GridSpec`创建大小相等的网格。你可以调整行和列的相对高度和宽度,要注意绝对高度值是无意义的,有意义的只是它们的相对比值。
153+
154+
```py
155+
gs = gridspec.GridSpec(2, 2,
156+
width_ratios=[1,2],
157+
height_ratios=[4,1]
158+
)
159+
160+
ax1 = plt.subplot(gs[0])
161+
ax2 = plt.subplot(gs[1])
162+
ax3 = plt.subplot(gs[2])
163+
ax4 = plt.subplot(gs[3])
164+
```
165+
166+
![](http://matplotlib.org/_images/demo_gridspec05.png)

0 commit comments

Comments
 (0)