3.3

涓婄骇 e9283187
# 浣跨敤 GridSpec 鑷畾涔夊瓙鍥句綅缃
> 鍘熸枃锛歔Customizing Location of Subplot Using GridSpec](http://matplotlib.org/users/gridspec.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
`GridSpec`
鎸囧畾瀛愬浘灏嗘斁缃殑缃戞牸鐨勫嚑浣曚綅缃 闇瑕佽缃綉鏍肩殑琛屾暟鍜屽垪鏁般 瀛愬浘甯冨眬鍙傛暟锛堜緥濡傦紝宸︼紝鍙崇瓑锛夊彲浠ラ夋嫨鎬ц皟鏁淬
`SubplotSpec`
鎸囧畾鍦ㄧ粰瀹`GridSpec`涓殑瀛愬浘浣嶇疆銆
`subplot2grid`
涓涓緟鍔╁嚱鏁帮紝绫讳技浜`pyplot.subplot`锛屼絾鏄娇鐢ㄥ熀浜 0 鐨勭储寮曪紝骞跺彲浣垮瓙鍥捐法瓒婂涓牸瀛愩
## `subplot2grid`鍩烘湰绀轰緥
瑕佷娇鐢╯ubplot2grid锛屼綘闇瑕佹彁渚涚綉鏍肩殑鍑犱綍褰㈢姸鍜岀綉鏍间腑瀛愬浘鐨勪綅缃 瀵逛簬绠鍗曠殑鍗曠綉鏍煎瓙鍥撅細
```py
ax = plt.subplot2grid((2,2),(0, 0))
```
绛変环浜庯細
```
ax = plt.subplot(2,2,1)
```
```
nRow=2, nCol=2
(0,0) +-------+-------+
| 1 | |
+-------+-------+
| | |
+-------+-------+
```
瑕佹敞鎰忎笉鎯`subplot``gridspec`涓殑涓嬫爣浠 0 寮濮嬨
涓轰簡鍒涘缓璺ㄨ秺澶氫釜鏍煎瓙鐨勫瓙鍥撅紝
```py
ax2 = plt.subplot2grid((3,3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
```
渚嬪锛屼笅鍒楀懡浠わ細
```py
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))
```
浼氬垱寤猴細
![](http://matplotlib.org/_images/demo_gridspec01.png)
## `GridSpec`鍜宍SubplotSpec`
浣犲彲浠ユ樉寮忓垱寤`GridSpec `骞剁敤瀹冧滑鍒涘缓瀛愬浘銆
渚嬪锛
```py
ax = plt.subplot2grid((2,2),(0, 0))
```
绛変环浜庯細
```py
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
```
`gridspec `绀轰緥鎻愪緵绫讳技鏁扮粍锛堜竴缁存垨浜岀淮锛夌殑绱㈠紩锛屽苟杩斿洖`SubplotSpec`瀹炰緥銆備緥濡傦紝浣跨敤鍒囩墖鏉ヨ繑鍥炶法瓒婂涓牸瀛愮殑`SubplotSpec`瀹炰緥銆
涓婇潰鐨勪緥瀛愪細鍙樻垚锛
```py
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])
```
![](http://matplotlib.org/_images/demo_gridspec02.png)
## 璋冩暣 `GridSpec`甯冨眬
鍦ㄦ樉寮忎娇鐢`GridSpec`鐨勬椂鍊欙紝浣犲彲浠ヨ皟鏁村瓙鍥剧殑甯冨眬鍙傛暟锛屽瓙鍥剧敱`gridspec`鍒涘缓銆
```py
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
```
杩欑被浼间簬`subplots_adjust`锛屼絾鏄粬鍙奖鍝嶄粠缁欏畾`GridSpec`鍒涘缓鐨勫瓙鍥俱
涓嬮潰鐨勪唬鐮
```py
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])
gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])
```
浼氫骇鐢
![](http://matplotlib.org/_images/demo_gridspec03.png)
## 浣跨敤 `SubplotSpec`鍒涘缓 `GridSpec`
浣犲彲浠ヤ粠`SubplotSpec`鍒涘缓`GridSpec`锛屽叾涓畠鐨勫竷灞鍙傛暟璁剧疆涓虹粰瀹`SubplotSpec`鐨勪綅缃殑甯冨眬鍙傛暟銆
```py
gs0 = gridspec.GridSpec(1, 2)
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
```
![](http://matplotlib.org/_images/demo_gridspec04.png)
## 浣跨敤`SubplotSpec`鍒涘缓澶嶆潅宓屽鐨刞GridSpec`
杩欓噷鏈変竴涓洿澶嶆潅鐨勫祵濂`gridspec`鐨勭ず渚嬶紝鎴戜滑閫氳繃鍦ㄦ瘡涓 3x3 鍐呴儴缃戞牸涓殣钘忛傚綋鐨勮剨绾匡紝鍦 4x4 澶栭儴缃戞牸鐨勬瘡涓崟鍏冩牸鍛ㄥ洿鏀剧疆涓涓銆
![](http://matplotlib.org/_images/demo_gridspec06.png)
## 缃戞牸灏哄鍙彉鐨刞GridSpec`
閫氬父锛`GridSpec`鍒涘缓澶у皬鐩哥瓑鐨勭綉鏍笺備綘鍙互璋冩暣琛屽拰鍒楃殑鐩稿楂樺害鍜屽搴︼紝瑕佹敞鎰忕粷瀵归珮搴﹀兼槸鏃犳剰涔夌殑锛屾湁鎰忎箟鐨勫彧鏄畠浠殑鐩稿姣斿笺
```py
gs = gridspec.GridSpec(2, 2,
width_ratios=[1,2],
height_ratios=[4,1]
)
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])
```
![](http://matplotlib.org/_images/demo_gridspec05.png)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽