3.4

涓婄骇 dab8b6f3
# 瀵嗚嚧甯冨眬鎸囧崡
> 鍘熸枃锛歔Tight Layout guide](http://matplotlib.org/users/tight_layout_guide.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
`tight_layout`浼氳嚜鍔ㄨ皟鏁村瓙鍥惧弬鏁帮紝浣夸箣濉厖鏁翠釜鍥惧儚鍖哄煙銆傝繖鏄釜瀹為獙鐗规э紝鍙兘鍦ㄤ竴浜涙儏鍐典笅涓嶅伐浣溿傚畠浠呬粎妫鏌ュ潗鏍囪酱鏍囩銆佸埢搴︽爣绛句互鍙婃爣棰樼殑閮ㄥ垎銆
## 绠鍗曠殑绀轰緥
鍦 matplotlib 涓紝杞村煙锛堝寘鎷瓙鍥撅級鐨勪綅缃互鏍囧噯鍖栧浘褰㈠潗鏍囨寚瀹氥 鍙兘鍙戠敓鐨勬槸锛屼綘鐨勮酱鏍囩鎴栨爣棰橈紙鏈夋椂鐢氳嚦鏄埢搴︽爣绛撅級浼氳秴鍑哄浘褰㈠尯鍩燂紝鍥犳琚埅鏂
```py
plt.rcParams['savefig.facecolor'] = "0.8"
def example_plot(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_ylabel('y-label', fontsize=fontsize)
ax.set_title('Title', fontsize=fontsize)
plt.close('all')
fig, ax = plt.subplots()
example_plot(ax, fontsize=24)
```
![](http://matplotlib.org/_images/tight_layout_guide-1.png)
涓轰簡閬垮厤瀹冿紝杞村煙鐨勪綅缃渶瑕佽皟鏁淬傚浜庡瓙鍥撅紝杩欏彲浠ラ氳繃璋冩暣瀛愬浘鍙傛暟锛[绉诲姩杞村煙鐨勪竴鏉¤竟鏉ョ粰鍒诲害鏍囩鑵惧湴鏂](http://matplotlib.org/faq/howto_faq.html#howto-subplots-adjust)锛夈侻atplotlib v1.1 寮曞叆浜嗕竴涓柊鐨勫懡浠` tight_layout()`锛岃嚜鍔ㄤ负浣犺В鍐宠繖涓棶棰樸
```py
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-2.png)
褰撲綘鎷ユ湁澶氫釜瀛愬浘鏃讹紝浣犱細缁忓父鐪嬪埌涓嶅悓杞村煙鐨勬爣绛惧彔鍦ㄤ竴璧枫
```py
plt.close('all')
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
```
![](http://matplotlib.org/_images/tight_layout_guide-3.png)
`tight_layout()`涔熶細璋冩暣瀛愬浘涔嬮棿鐨勯棿闅旀潵鍑忓皯鍫嗗彔銆
![](http://matplotlib.org/_images/tight_layout_guide-4.png)
`tight_layout()`鍙互鎺ュ彈鍏抽敭瀛楀弬鏁`pad``w_pad`鎴栬`h_pad`锛岃繖浜涘弬鏁板浘鍍忚竟鐣屽拰瀛愬浘涔嬮棿鐨勯澶栬竟璺濄傝竟璺濅互瀛椾綋澶у皬鍗曚綅瑙勫畾銆
```py
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
```
![](http://matplotlib.org/_images/tight_layout_guide-5.png)
鍗充娇瀛愬浘澶у皬涓嶅悓锛`tight_layout()`涔熻兘澶熷伐浣滐紝鍙缃戞牸鐨勮瀹氱殑鍏煎鐨勩傚湪涓嬮潰鐨勪緥瀛愪腑锛`ax1``ax2`鏄 2x2 缃戞牸鐨勫瓙鍥撅紝浣嗘槸`ax3`鏄 1x2 缃戞牸銆
```py
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-6.png)
瀹冮傜敤浜庝娇鐢`subplot2grid()`鍒涘缓鐨勫瓙鍥俱 涓鑸潵璇达紝浠`gridspec`[浣跨敤`GridSpec`鑷畾涔夊瓙甯冨眬鐨勪綅缃](http://matplotlib.org/users/gridspec.html#gridspec-guide)锛夊垱寤虹殑瀛愬浘涔熻兘姝e父宸ヤ綔銆
```py
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-7.png)
铏界劧娌℃湁褰诲簳娴嬭瘯锛屽畠鐪嬭捣鏉ヤ篃閫傜敤浜`aspect`涓嶄负`auto`鐨勫瓙鍥撅紙渚嬪甯︽湁鍥惧儚鐨勮酱鍩燂級銆
```py
arr = np.arange(100).reshape((10,10))
plt.close('all')
fig = plt.figure(figsize=(5,4))
ax = plt.subplot(111)
im = ax.imshow(arr, interpolation="none")
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-8.png)
## 璀﹀憡
+ `tight_layout()`鍙冭檻鍒诲害鏍囩锛岃酱鏍囩鍜屾爣棰樸 鍥犳锛屽叾浠 Artist 鍙兘琚埅鏂苟涓斾篃鍙兘閲嶅彔銆
+ 瀹冨亣瀹氬埢搴︽爣绛撅紝杞存爣绛惧拰鏍囬鎵闇鐨勯澶栫┖闂翠笌杞村煙鐨勫師濮嬩綅缃棤鍏炽 杩欓氬父鏄湡鐨勶紝浣嗗湪缃曡鐨勬儏鍐典笅涓嶆槸銆
+ `pad = 0`灏嗘煇浜涙枃鏈壀鍒囧嚑涓儚绱犮 杩欏彲鑳芥槸褰撳墠绠楁硶鐨勯敊璇垨闄愬埗锛屽苟涓斾笉娓呮涓轰粈涔堜細鍙戠敓銆 鍚屾椂锛屾帹鑽愪娇鐢ㄨ嚦灏戝ぇ浜 0.3 鐨勯棿闅斻
## 鍜宍GridSpec`涓璧蜂娇鐢
`GridSpec`鎷ユ湁鑷繁鐨`tight_layout()`鏂规硶锛坧yplot API 鐨`tight_layout()`涔熻兘鐢熸晥锛夈
```py
plt.close('all')
fig = plt.figure()
import matplotlib.gridspec as gridspec
gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
example_plot(ax1)
example_plot(ax2)
gs1.tight_layout(fig)
```
![](http://matplotlib.org/_images/tight_layout_guide-9.png)
浣犲彲浠ユ彁渚涗竴涓彲閫夌殑`rect`鍙傛暟锛屾寚瀹氬瓙鍥炬墍濉厖鐨勮竟妗嗐 鍧愭爣蹇呴』涓烘爣鍑嗗寲鍥惧舰鍧愭爣锛岄粯璁ゅ间负`(0, 0, 1, 1)`
```py
gs1.tight_layout(fig, rect=[0, 0, 0.5, 1])
```
![](http://matplotlib.org/_images/tight_layout_guide-10.png)
渚嬪锛岃繖鍙敤浜庡甫鏈夊涓`gridspecs`鐨勫浘褰€
```py
gs2 = gridspec.GridSpec(3, 1)
for ss in gs2:
ax = fig.add_subplot(ss)
example_plot(ax)
ax.set_title("")
ax.set_xlabel("")
ax.set_xlabel("x-label", fontsize=12)
gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)
```
![](http://matplotlib.org/_images/tight_layout_guide-11.png)
鎴戜滑鍙互灏濊瘯鍖归厤涓や釜缃戞牸鐨勯《閮ㄥ拰搴曢儴銆
```py
top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)
gs1.update(top=top, bottom=bottom)
gs2.update(top=top, bottom=bottom)
```
铏界劧杩欏簲璇ヨ冻澶熷ソ浜嗭紝璋冩暣椤堕儴鍜屽簳閮ㄥ彲鑳戒篃闇瑕佽皟鏁`hspace`銆 涓轰簡鏇存柊`hspace``vspace`锛屾垜浠啀娆′娇鐢ㄦ洿鏂板悗鐨`rect`鍙傛暟璋冪敤`tight_layout()`銆 娉ㄦ剰锛`rect`鍙傛暟鎸囧畾鐨勫尯鍩熷寘鎷埢搴︽爣绛俱傚洜姝わ紝鎴戜滑灏嗗簳閮紙姝e父鎯呭喌涓嬩负 0锛夊鍔犳瘡涓`gridspec`鐨勫簳閮ㄤ箣宸 椤堕儴涔熶竴鏍枫
```py
top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)
gs1.tight_layout(fig, rect=[None, 0 + (bottom-gs1.bottom),
0.5, 1 - (gs1.top-top)])
gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom),
None, 1 - (gs2.top-top)],
h_pad=0.5)
```
![](http://matplotlib.org/_images/tight_layout_guide-12.png)
## 鍜宍AxesGrid1`涓璧蜂娇鐢
铏界劧鍙楅檺浣嗕篃鏀寔`axes_grid1`宸ュ叿鍖
```py
plt.close('all')
fig = plt.figure()
from mpl_toolkits.axes_grid1 import Grid
grid = Grid(fig, rect=111, nrows_ncols=(2,2),
axes_pad=0.25, label_mode='L',
)
for ax in grid:
example_plot(ax)
ax.title.set_visible(False)
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-13.png)
## 棰滆壊鏉
濡傛灉浣犱娇鐢`colorbar`鍛戒护鍒涘缓浜嗛鑹叉潯锛屽垱寤虹殑棰滆壊鏉℃槸`Axes`鑰屼笉鏄`Subplot`鐨勫疄渚嬶紝鎵浠`tight_layout`娌℃湁鏁堟灉銆傚湪 Matplotlib v1.1 涓紝浣犲彲浠ヤ娇鐢`gridspec`灏嗛鑹叉潯鍒涘缓涓哄瓙鍥俱
```py
plt.close('all')
arr = np.arange(100).reshape((10,10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation="none")
plt.colorbar(im, use_gridspec=True)
plt.tight_layout()
```
![])http://matplotlib.org/_images/tight_layout_guide-14.png
鍙︿竴涓夐」鏄娇鐢`AxesGrid1`宸ュ叿鍖咃紝鏄惧紡涓洪鑹叉潯鍒涘缓涓涓酱鍩燂細
```py
plt.close('all')
arr = np.arange(100).reshape((10,10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation="none")
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", "5%", pad="3%")
plt.colorbar(im, cax=cax)
plt.tight_layout()
```
![](http://matplotlib.org/_images/tight_layout_guide-15.png)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽