3.7

涓婄骇 286d3508
# 鍙樻崲鏁欑▼
> 鍘熸枃锛歔Transformations Tutorial](http://matplotlib.org/users/transforms_tutorial.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
鍍忎换浣曞浘褰㈠寘涓鏍凤紝matplotlib 寤虹珛鍦ㄥ彉鎹㈡鏋朵箣涓婏紝浠ヤ究鍦ㄥ潗鏍囩郴锛岀敤鎴锋暟鎹潗鏍囩郴锛岃酱鍩熷潗鏍囩郴锛屽浘褰㈠潗鏍囩郴鍜屾樉绀哄潗鏍囩郴涔嬮棿杞绘槗鍙樻崲銆 鍦 95 锛呯殑缁樺浘涓紝浣犱笉闇瑕佽冭檻杩欎竴鐐癸紝鍥犱负瀹冨彂鐢熷湪鑳屽悗锛屼絾闅忕潃浣犳帴杩戣嚜瀹氫箟鍥惧舰鐢熸垚鐨勬瀬闄愶紝瀹冩湁鍔╀簬鐞嗚В杩欎簺瀵硅薄锛屼互渚垮彲浠ラ噸鐢 matplotlib 鎻愪緵缁欎綘鐨勭幇鏈夊彉鎹紝鎴栬呭垱寤鸿嚜宸辩殑鍙樻崲锛堣`matplotlib.transforms`锛夈 涓嬭〃鎬荤粨浜嗙幇鏈夌殑鍧愭爣绯伙紝浣犲簲璇ュ湪璇ュ潗鏍囩郴涓娇鐢ㄧ殑鍙樻崲瀵硅薄锛屼互鍙婅绯荤粺鐨勬弿杩般 鍦ㄣ庡彉鎹㈠璞°忎竴鍒椾腑锛`ax``Axes`瀹炰緥锛`fig`鏄竴涓浘褰㈠疄渚嬨
| 鍧愭爣绯 | 鍙樻崲瀵硅薄 | 鎻忚堪 |
| --- | --- | --- |
| 鏁版嵁 | `ax.transData` | 鐢ㄦ埛鏁版嵁鍧愭爣绯伙紝鐢`xlim``ylim`鎺у埗 |
| 杞村煙 | `ax.transAxes` | 杞村煙鍧愭爣绯伙紱`(0,0)`鏄酱鍩熷乏涓嬭锛`(1,1)`鏄酱鍩熷彸涓婅 |
| 鍥惧舰 | `fig.transFigure` | 鍥惧舰鍧愭爣绯伙紱`(0,0)`鏄浘褰㈠乏涓嬭锛`(1,1)`鏄浘褰㈠彸涓婅 |
| 鏄剧ず | `None` | 杩欐槸鏄剧ず鍣ㄧ殑鍍忕礌鍧愭爣绯; `(0,0)`鏄樉绀哄櫒鐨勫乏涓嬭锛`(width, height)`鏄樉绀哄櫒鐨勫彸涓婅锛屼互鍍忕礌涓哄崟浣嶃 鎴栬咃紝鍙互浣跨敤鎭掔瓑鍙樻崲锛`matplotlib.transforms.IdentityTransform()`锛夋潵浠f浛`None`銆 |
涓婅〃涓殑鎵鏈夊彉鎹㈠璞¢兘鎺ュ彈浠ュ叾鍧愭爣绯讳负鍗曚綅鐨勮緭鍏ワ紝骞跺皢杈撳叆鍙樻崲鍒版樉绀哄潗鏍囩郴銆 杩欏氨鏄负浠涔堟樉绀哄潗鏍囩郴娌℃湁銆庡彉鎹㈠璞°忕殑鍘熷洜 - 瀹冨凡缁忎互鏄剧ず鍧愭爣涓哄崟浣嶄簡銆 鍙樻崲涔熺煡閬撳浣曞弽杞嚜韬紝浠庢樉绀鸿繑鍥炶嚜韬殑鍧愭爣绯汇 杩欏湪澶勭悊鏉ヨ嚜鐢ㄦ埛鐣岄潰鐨勪簨浠讹紙閫氬父鍙戠敓鍦ㄦ樉绀虹┖闂翠腑锛夛紝骞朵笖浣犳兂鐭ラ亾鏁版嵁鍧愭爣绯讳腑榧犳爣鐐瑰嚮鎴栨寜閿寜涓嬬殑浣嶇疆鏃剁壒鍒湁鐢ㄣ
## 鏁版嵁鍧愭爣
璁╂垜浠粠鏈甯哥敤鐨勫潗鏍囷紝鏁版嵁鍧愭爣绯诲紑濮嬨 姣忓綋鍚戣酱鍩熸坊鍔犳暟鎹椂锛宮atplotlib 浼氭洿鏂版暟鎹璞★紝`set_xlim()``set_ylim()`鏂规硶鏈甯哥敤浜庢洿鏂般 渚嬪锛屽湪涓嬪浘涓紝鏁版嵁鐨勮寖鍥村湪`x`杞翠笂涓轰粠 0 鍒 10锛屽湪`y`杞翠笂涓轰粠 -1 鍒 1銆
```py
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
plt.show()
```
![](http://matplotlib.org/_images/transforms_tutorial-1.png)
浣犲彲浠ヤ娇鐢`ax.transData`瀹炰緥灏嗘暟鎹彉鎹负鏄剧ず鍧愭爣绯伙紝鏃犺鏄崟涓偣鎴栨槸涓绯诲垪鐐癸紝濡備笅鎵绀猴細
```py
In [14]: type(ax.transData)
Out[14]: <class 'matplotlib.transforms.CompositeGenericTransform'>
In [15]: ax.transData.transform((5, 0))
Out[15]: array([ 335.175, 247. ])
In [16]: ax.transData.transform([(5, 0), (1,2)])
Out[16]:
array([[ 335.175, 247. ],
[ 132.435, 642.2 ]])
```
浣犲彲浠ヤ娇鐢`inverted()`鏂规硶鍒涘缓涓涓彉鎹紝浠庢樉绀哄潗鏍囧彉鎹负鏁版嵁鍧愭爣锛
```py
In [41]: inv = ax.transData.inverted()
In [42]: type(inv)
Out[42]: <class 'matplotlib.transforms.CompositeGenericTransform'>
In [43]: inv.transform((335.175, 247.))
Out[43]: array([ 5., 0.])
```
濡傛灉浣犱竴鐩村叧娉ㄦ湰鏁欑▼锛屽鏋滀綘鐨勭獥鍙eぇ灏忔垨 dpi 璁剧疆涓嶅悓锛屾樉绀哄潗鏍囩殑纭垏鍊煎彲鑳戒細鏈夋墍涓嶅悓銆 鍚屾牱锛屽湪涓嬮潰鐨勫浘褰腑锛屽湪 ipython 浼氳瘽涓紝鐢辨樉绀烘爣璁扮殑鐐瑰彲鑳藉苟涓嶇浉鍚岋紝鍥犱负鏂囨。鍥惧舰澶у皬榛樿鍊兼槸涓嶅悓鐨勩
![](http://matplotlib.org/_images/annotate_transform.png)
> 娉ㄦ剰
> 濡傛灉鍦 GUI 鍚庣涓繍琛屼笂杩扮ず渚嬩腑鐨勬簮浠g爜锛屼綘杩樺彲鑳藉彂鐜版暟鎹拰鏄剧ず鏍囨敞鐨勪袱涓澶翠笉浼氭寚鍚戝畬鍏ㄧ浉鍚岀殑鐐广 杩欐槸鍥犱负鏄剧ず鐐规槸鍦ㄦ樉绀哄浘褰箣鍓嶈绠楃殑锛屽苟涓 GUI 鍚庣鍙互鍦ㄥ垱寤哄浘褰㈡椂绋嶅井璋冩暣鍥惧舰澶у皬銆 濡傛灉浣犺嚜宸辫皟鏁村浘鐨勫ぇ灏忥紝鏁堟灉鏇存槑鏄俱 杩欐槸浣犲緢灏戞兂瑕佸鐞嗘樉绀虹┖闂寸殑涓涓緢濂界殑鍘熷洜锛屼絾鏄綘鍙互杩炴帴鍒癭'on_draw'`浜嬩欢鏉ユ洿鏂板浘涓婄殑鍥惧潗鏍囷紱璇峰弬闃匸浜嬩欢澶勭悊鍜岄夋嫨](http://matplotlib.org/users/event_handling.html#event-handling-tutorial)銆
褰撲綘鏇存敼杞寸殑`x``y`鐨勮寖鍥存椂锛屽皢鏇存柊鏁版嵁鑼冨洿锛屼互渚垮彉鎹㈢敓鎴愭柊鐨勬樉绀虹偣銆 娉ㄦ剰锛屽綋鎴戜滑鍙槸鏀瑰彉`ylim`锛屽彧鏈`y`鏄剧ず鍧愭爣鏀瑰彉锛屽綋鎴戜滑鏀瑰彉`xlim`涔熷悓鐞嗐 鎴戜滑鍦ㄨ皥璁 [Bbox](http://matplotlib.org/api/transformations.html#matplotlib.transforms.Bbox) 鏃朵細娣卞叆銆
```py
In [54]: ax.transData.transform((5, 0))
Out[54]: array([ 335.175, 247. ])
In [55]: ax.set_ylim(-1,2)
Out[55]: (-1, 2)
In [56]: ax.transData.transform((5, 0))
Out[56]: array([ 335.175 , 181.13333333])
In [57]: ax.set_xlim(10,20)
Out[57]: (10, 20)
In [58]: ax.transData.transform((5, 0))
Out[58]: array([-171.675 , 181.13333333])
```
## 杞村煙鍧愭爣
鍦ㄦ暟鎹潗鏍囩郴涔嬪悗锛岃酱鍩熷彲鑳芥槸绗簩鏈夌敤鐨勫潗鏍囩郴銆 杩欓噷锛岀偣`(0,0)`鏄酱鍩熸垨瀛愬浘鐨勫乏涓嬭锛`(0.5,0.5)`鏄腑蹇冿紝`(1.0,1.0)`鏄彸涓婅銆 浣犺繕鍙互寮曠敤鑼冨洿涔嬪鐨勭偣锛屽洜姝`(-0.1,1.1)`浣嶄簬杞寸殑宸︿笂鏂广 姝ゅ潗鏍囩郴鍦ㄥ皢鏂囨湰鏀剧疆鍦ㄨ酱涓椂闈炲父鏈夌敤锛屽洜涓轰綘閫氬父闇瑕佸湪鍥哄畾鐨勪綅缃紙渚嬪锛岃酱鍩熺獥鏍肩殑宸︿笂瑙掞級鏀剧疆鏂囨湰姘旀场锛屽苟涓斿湪骞崇Щ鎴栫缉鏀炬椂淇濇寔璇ヤ綅缃浐瀹氥 杩欓噷鏄竴涓畝鍗曠殑渚嬪瓙锛屽垱寤哄洓涓潰鏉匡紝骞跺皢浠栦滑鏍囪涓`'A'``'B'``'C'``'D'`锛屼綘缁忓父鍦ㄦ湡鍒婁笂鐪嬪埌瀹冧滑銆
![](http://matplotlib.org/_images/transforms_tutorial-2.png)
浣犱篃鍙互鍦ㄨ酱鍧愭爣绯讳腑鍒涘缓绾挎潯鎴栬呰ˉ涓侊紝浣嗘槸浠ユ垜鐨勭粡楠岋紝杩欐瘮浣跨敤`ax.transAxes`鏀剧疆鏂囨湰鏇翠笉瀹炵敤銆 灏界濡傛锛岃繖閲屾槸涓涓剼锠㈢殑渚嬪瓙锛屽畠鍦ㄦ暟鎹┖闂翠腑缁樺埗浜嗕竴浜涢殢鏈虹偣锛屽苟涓旇鐩栧湪涓涓崐閫忔槑鐨勫渾涓婇潰锛岃繖涓渾浠ヨ酱鍩熺殑涓績涓哄渾蹇冿紝鍗婂緞涓鸿酱鍩熺殑鍥涘垎涔嬩竴銆 - 濡傛灉浣犵殑杞村煙涓嶄繚鐣欓珮瀹芥瘮锛堣`set_aspect ()`锛夛紝瀹冨皢鐪嬭捣鏉ュ儚涓涓き鍦嗐 浣跨敤骞崇Щ/缂╂斁宸ュ叿绉诲姩锛屾垨鎵嬪姩鏇存敼鏁版嵁鐨`xlim``ylim`锛屼綘灏嗙湅鍒版暟鎹Щ鍔紝浣嗗渾灏嗕繚鎸佸浐瀹氾紝鍥犱负瀹冧笉鍦ㄦ暟鎹潗鏍囦腑锛屽苟涓斿皢濮嬬粓淇濇寔鍦ㄨ酱鍩熺殑涓績 銆
![](http://matplotlib.org/_images/transforms_tutorial-3.png)
## 娣峰悎鍙樻崲
鍦ㄦ暟鎹笌杞村煙鍧愭爣娣峰悎鐨勬贩鍚堝潗鏍囩┖闂翠腑缁樺埗鏄潪甯稿疄鐢ㄧ殑锛屼緥濡傚垱寤轰竴涓按骞宠法搴︼紝绐佸嚭`y`鏁版嵁鐨勪竴浜涘尯鍩熶絾妯法`x`杞达紝鑰屾棤璁烘暟鎹檺鍒讹紝骞崇Щ鎴栫缉鏀剧骇鍒瓑銆傚疄闄呬笂杩欎簺娣峰悎绾挎潯鍜岃法搴﹂潪甯告湁鐢紝鎴戜滑宸茬粡鍐呯疆浜嗕竴浜涘嚱鏁版潵浣垮畠浠鏄撶粯鍒讹紙鍙傝`axhline()``axvline()``axhspan()``axvspan()`锛夛紝浣嗘槸涓轰簡鏁欏鐩殑锛屾垜浠娇鐢ㄦ贩鍚堝彉鎹㈠疄鐜拌繖閲岀殑姘村钩璺ㄥ害銆 杩欎釜鎶宸у彧閫傜敤浜庡彲鍒嗙鐨勫彉鎹紝灏卞儚浣犲湪姝e父鐨勭瑳鍗″皵鍧愭爣绯讳腑鐪嬪埌鐨勶紝浣嗕笉鑳戒负涓嶅彲鍒嗙鐨勫彉鎹紝濡`PolarTransform`锛堟瀬鍧愭爣鍙樻崲锛夈
```py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.random.randn(1000)
ax.hist(x, 30)
ax.set_title(r'$\sigma=1 \/ \dots \/ \sigma=2$', fontsize=16)
# the x coords of this transformation are data, and the
# y coord are axes
trans = transforms.blended_transform_factory(
ax.transData, ax.transAxes)
# highlight the 1..2 stddev region with a span.
# We want x to be in data coordinates and y to
# span from 0..1 in axes coords
rect = patches.Rectangle((1,0), width=1, height=1,
transform=trans, color='yellow',
alpha=0.5)
ax.add_patch(rect)
plt.show()
```
![](http://matplotlib.org/_images/transforms_tutorial-4.png)
> 娉
> 娣峰悎鍙樻崲闈炲父鏈夌敤锛屽叾涓璥x`涓烘暟鎹潗鏍囪宍y`涓鸿酱鍩熷潗鏍囷紝鎴戜滑鎷ユ湁杈呭姪鏂规硶鏉ヨ繑鍥炲唴閮ㄤ娇鐢ㄧ殑鐗堟湰 mpl 锛岀敤浜庣粯鍒禶ticks`锛宍ticklabels`浠ュ強鍏朵粬銆傛柟娉曟槸`matplotlib.axes.Axes.get_xaxis_transform()`鍜宍matplotlib.axes.Axes.get_yaxis_transform()`銆 鍥犳锛屽湪涓婇潰鐨勭ず渚嬩腑锛宍blended_transform_factory()`鐨勮皟鐢ㄥ彲浠ユ浛鎹负`get_xaxis_transform`锛
> ```py
> trans = ax.get_xaxis_transform()
> ```
## 浣跨敤鍋忕Щ鍙樻崲鏉ュ垱寤洪槾褰辨晥鏋
鍙樻崲鐨勪竴涓敤娉曪紝鏄垱寤哄亸绂诲彟涓鍙樻崲鐨勬柊鍙樻崲锛屼緥濡傦紝鏀剧疆涓涓璞★紝鐩稿浜庡彟涓瀵硅薄鏈変竴浜涘亸绉汇 閫氬父锛屼綘甯屾湜鐗╃悊灏哄涓婃湁涓浜涚Щ浣嶏紝渚嬪浠ョ偣鎴栬嫳瀵革紝鑰屼笉鏄暟鎹潗鏍囦负鍗曚綅锛屼互渚跨Щ浣嶆晥鏋滃湪涓嶅悓鐨勭缉鏀剧骇鍒拰 dpi 璁剧疆涓嬩繚鎸佷笉鍙樸
鍋忕Щ鐨勪竴涓敤閫旀槸鍒涘缓涓涓槾褰辨晥鏋滐紝鍏朵腑浣犵粯鍒朵竴涓笌绗竴涓浉鍚岀殑瀵硅薄锛屽垰濂藉湪瀹冪殑鍙宠竟鍜屼笅闈紝璋冩暣`zorder`鏉ョ‘淇濋鍏堢粯鍒堕槾褰憋紝鐒跺悗缁樺埗瀵硅薄锛岄槾褰卞湪瀹冧箣涓娿 鍙樻崲妯″潡鍏锋湁杈呭姪鍙樻崲`ScaledTranslation`銆 瀹冨彲浠ヨ繖鏍锋潵瀹炰緥鍖栵細
```py
trans = ScaledTranslation(xt, yt, scale_trans)
```
鍏朵腑`xt``yt`鏄彉鎹㈢殑鍋忕Щ锛`scale_trans`鏄彉鎹紝鍦ㄥ簲鐢ㄥ亸绉讳箣鍓嶇殑鍙樻崲鏈熼棿缂╂斁`xt``yt`銆 涓涓吀鍨嬬殑鐢ㄤ緥鏄紝灏嗗浘褰㈢殑`fig.dpi_scale_trans`鍙樻崲鐢ㄤ簬`scale_trans`鍙傛暟锛屾潵鍦ㄥ疄鐜版渶缁堢殑鍋忕Щ涔嬪墠锛岄鍏堝皢浠ョ偣涓哄崟浣嶇殑`xt``yt`缂╂斁鍒版樉绀虹┖闂淬
DPI 鍜岃嫳瀵稿亸绉绘槸甯歌鐨勭敤渚嬶紝鎴戜滑鎷ユ湁涓涓壒娈婄殑杈呭姪鍑芥暟锛屾潵鍦`matplotlib.transforms.offset_copy()`涓垱寤哄畠锛屽畠杩斿洖涓涓甫鏈夐檮鍔犲亸绉荤殑鏂板彉鎹€ 浣嗗湪涓嬮潰鐨勭ず渚嬩腑锛屾垜浠皢鑷繁鍒涘缓鍋忕Щ鍙樻崲銆 娉ㄦ剰浣跨敤鍔犳硶杩愮畻绗︼細
```py
offset = transforms.ScaledTranslation(dx, dy,
fig.dpi_scale_trans)
shadow_transform = ax.transData + offset
```
杩欓噷鏄剧ず浜嗭紝鍙互浣跨敤鍔犳硶杩愮畻绗﹀皢鍙樻崲閾捐捣鏉ャ 璇ヤ唬鐮佽〃绀猴細棣栧厛搴旂敤鏁版嵁鍙樻崲`ax.transData`锛岀劧鍚庣敱`dx``dy`鐐圭炕璇戞暟鎹 鍦[鎺掔増](https://en.wikipedia.org/wiki/Point_%28typography%29)涓紝涓涓偣鏄 1/72 鑻卞锛岄氳繃浠ョ偣涓哄崟浣嶆寚瀹氬亸绉伙紝浣犵殑鍥惧舰鐪嬭捣鏉ユ槸涓鏍风殑锛屾棤璁烘墍淇濆瓨鐨 dpi 鍒嗚鲸鐜囥
```py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms
fig = plt.figure()
ax = fig.add_subplot(111)
# make a simple sine wave
x = np.arange(0., 2., 0.01)
y = np.sin(2*np.pi*x)
line, = ax.plot(x, y, lw=3, color='blue')
# shift the object over 2 points, and down 2 points
dx, dy = 2/72., -2/72.
offset = transforms.ScaledTranslation(dx, dy,
fig.dpi_scale_trans)
shadow_transform = ax.transData + offset
# now plot the same data with our offset transform;
# use the zorder to make sure we are below the line
ax.plot(x, y, lw=3, color='gray',
transform=shadow_transform,
zorder=0.5*line.get_zorder())
ax.set_title('creating a shadow effect with an offset transform')
plt.show()
```
![](http://matplotlib.org/_images/transforms_tutorial-5.png)
## 鍙樻崲娴佹按绾
鎴戜滑鍦ㄦ湰鏁欑▼涓竴鐩翠娇鐢ㄧ殑`ax.transData`鍙樻崲鏄笁绉嶄笉鍚屽彉鎹㈢殑缁勫悎锛屽畠浠瀯鎴愪粠鏁版嵁鍒版樉绀哄潗鏍囩殑鍙樻崲娴佹按绾裤 Michael Droettboom 瀹炵幇浜嗗彉鎹㈡鏋讹紝鎻愪緵浜嗕竴涓共鍑鐨 API锛屽畠闅旂浜嗗湪鏋佸潗鏍囧拰瀵规暟鍧愭爣鍥句腑鍙戠敓鐨勯潪绾挎ф姇褰卞拰灏哄害锛屼互鍙婂湪骞崇Щ鍜岀缉鏀炬椂鍙戠敓鐨勭嚎鎬т豢灏勫彉鎹€ 杩欓噷鏈変竴涓晥鐜囬棶棰橈紝鍥犱负浣犲彲浠ュ钩绉诲拰鏀惧ぇ浣犵殑杞村煙锛屽畠浼氬奖鍝嶄豢灏勫彉鎹紝浣嗕綘鍙兘涓嶉渶瑕佽绠楁綔鍦ㄧ殑鏄傝吹鐨勯潪绾挎ф瘮渚嬫垨绠鍗曠殑瀵艰埅浜嬩欢鐨勬姇褰便 涔熷彲浠ュ皢浠垮皠鍙樻崲鐭╅樀鐩镐箻鍦ㄤ竴璧凤紝鐒跺悗鍦ㄤ竴姝ヤ箣涓皢瀹冧滑搴旂敤浜庡潗鏍囥 杩欏鎵鏈夊彲鑳界殑鍙樻崲涓嶉兘鏄湁鏁堢殑銆
杩欓噷鏄湪`ax.transData`瀹炰緥鍦ㄥ熀鏈彲鍒嗙鐨`Axes`绫讳腑鐨勫畾涔夋柟寮忋
```py
self.transData = self.transScale + (self.transLimits + self.transAxes)
```
鎴戜滑宸茬粡鍦`Axes`鍧愭爣涓紩鍏ヤ簡涓婇潰鐨`transAxes`瀹炰緥锛屽畠灏嗚酱鎴栧瓙鍥捐竟鐣屾鐨`(0,0)``(1,1)`瑙掓槧灏勫埌鏄剧ず绌洪棿锛屾墍浠ヨ鎴戜滑鐪嬬湅杩欎袱涓儴鍒嗐
`self.transLimits`鏄粠鏁版嵁鍒拌酱鍩熷潗鏍囩殑鍙樻崲; 涔熷氨鏄锛屽畠灏嗕綘鐨勮鍥`xlim``ylim`鏄犲皠鍒拌酱鍩熷崟浣嶇┖闂达紙鐒跺悗`transAxes`灏嗚鍗曚綅绌洪棿鐢ㄤ簬鏄剧ず绌洪棿锛夈 鎴戜滑鍙互鍦ㄨ繖閲岀湅鍒拌繖涓鐐癸細
```py
In [80]: ax = subplot(111)
In [81]: ax.set_xlim(0, 10)
Out[81]: (0, 10)
In [82]: ax.set_ylim(-1,1)
Out[82]: (-1, 1)
In [84]: ax.transLimits.transform((0,-1))
Out[84]: array([ 0., 0.])
In [85]: ax.transLimits.transform((10,-1))
Out[85]: array([ 1., 0.])
In [86]: ax.transLimits.transform((10,1))
Out[86]: array([ 1., 1.])
In [87]: ax.transLimits.transform((5,0))
Out[87]: array([ 0.5, 0.5])
```
鑰屼笖鎴戜滑鍙互浣跨敤鐩稿悓鐨勫弽杞彉鎹紝浠庤酱鍩熷崟浣嶅潗鏍囧彉鎹㈠洖鏁版嵁鍧愭爣銆
```py
In [90]: inv.transform((0.25, 0.25))
Out[90]: array([ 2.5, -0.5])
```
鏈鍚庝竴涓槸`self.transScale`灞炴э紝瀹冭礋璐f暟鎹殑鍙夐潪绾挎х缉鏀撅紝渚嬪瀵规暟杞村煙銆 褰`Axes`鍒濆鍖栨椂锛岃繖鍙槸璁剧疆涓烘亽绛夊彉鎹紝鍥犱负鍩烘湰鐨 matplotlib 杞村煙鍏锋湁绾挎х缉鏀撅紝浣嗘槸褰撲綘璋冪敤瀵规暟缂╂斁鍑芥暟濡`semilogx()`鎴栦娇鐢`set_xscale`鏄惧紡璁剧疆涓哄鏁版椂锛`ax.transScale`灞炴т负澶勭悊闈炵嚎鎬ф姇褰辫岃缃 缂╂斁鍙樻崲鏄浉搴`xaxis``yaxis``Axis`瀹炰緥鐨勫睘鎬с 渚嬪锛屽綋璋冪敤`ax.set_xscale('log')`鏃讹紝`xaxis`浼氬皢鍏剁缉鏀炬洿鏂颁负`matplotlib.scale.LogScale`瀹炰緥銆
瀵逛簬涓嶅彲鍒嗙鐨勮酱鍩燂紝`PolarAxes`锛岃繕鏈変竴涓鑰冭檻鐨勯儴鍒嗭紝鎶曞奖鍙樻崲銆 `matplotlib.projections.polar.PolarAxes``transData`绫讳技浜庡吀鍨嬬殑鍙垎绂 matplotlib 杞村煙锛屽甫鏈変竴涓澶栫殑閮ㄥ垎锛`transProjection`
```py
self.transData = self.transScale + self.transProjection + \
(self.transProjectionAffine + self.transAxes)
```
`transProjection`灏嗘潵鑷┖闂寸殑鎶曞奖锛屼緥濡傦紝鍦板浘鏁版嵁鐨勭含搴﹀拰缁忓害锛屾垨鏋佸潗鏍囨暟鎹殑鍗婂緞鍜屾瀬瑙掞紝澶勭悊涓哄彲鍒嗙鐨勭瑳鍗″皵鍧愭爣绯汇 鍦`matplotlib.projections`鍖呬腑鏈夊嚑涓姇褰辩ず渚嬶紝娣卞叆浜嗚В鐨勬渶濂芥柟娉曟槸鎵撳紑杩欎簺鍖呯殑婧愪唬鐮侊紝鐪嬬湅濡備綍鑷繁鍒朵綔瀹冿紝鍥犱负 matplotlib 鏀寔鍙墿灞曠殑杞村煙鍜屾姇褰便 Michael Droettboom 鎻愪緵浜嗕竴涓垱寤轰竴涓敜鎶曞奖杞村煙鐨勫緢濂界殑鏁欑▼绀轰緥锛涜鍙傞槄 api 绀轰緥浠g爜锛[`custom_projection_example.py`](http://matplotlib.org/examples/api/custom_projection_example.html#api-custom-projection-example)
......@@ -6,6 +6,73 @@
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
## 鍩烘湰鏍囨敞
浣跨敤`text()`浼氬皢鏂囨湰鏀剧疆鍦ㄨ酱鍩熺殑浠绘剰浣嶇疆銆 鏂囨湰鐨勪竴涓父瑙佺敤渚嬫槸鏍囨敞缁樺浘鐨勬煇浜涚壒寰侊紝鑰`annotate()`鏂规硶鎻愪緵杈呭姪鍑芥暟锛屼娇鏍囨敞鍙樺緱瀹规槗銆 鍦ㄦ爣娉ㄤ腑锛屾湁涓や釜瑕佽冭檻鐨勭偣锛氱敱鍙傛暟`xy`琛ㄧず鐨勬爣娉ㄤ綅缃拰`xytext`鐨勬枃鏈綅缃 杩欎袱涓弬鏁伴兘鏄`(x, y)`鍏冪粍銆
```py
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05),
)
ax.set_ylim(-2,2)
plt.show()
```
![](http://matplotlib.org/_images/annotation_basic.png)
鍦ㄨ绀轰緥涓紝`xy`锛堢澶村皷绔級鍜`xytext`浣嶇疆锛堟枃鏈綅缃級閮戒互鏁版嵁鍧愭爣涓哄崟浣嶃 鏈夊绉嶅彲浠ラ夋嫨鐨勫叾浠栧潗鏍囩郴 - 浣犲彲浠ヤ娇鐢`xycoords``textcoords`浠ュ強涓嬪垪瀛楃涓蹭箣涓锛堥粯璁や负`data`锛夋寚瀹`xy``xytext`鐨勫潗鏍囩郴銆
| 鍙傛暟 | 鍧愭爣绯 |
| `'figure points'` | 璺濈鍥惧舰宸︿笅瑙掔殑鐐规暟閲 |
| `'figure pixels'` | 璺濈鍥惧舰宸︿笅瑙掔殑鍍忕礌鏁伴噺 |
| `'figure fraction'` | 0,0 鏄浘褰㈠乏涓嬭锛1,1 鏄彸涓婅 |
| `'axes points'` | 璺濈杞村煙宸︿笅瑙掔殑鐐规暟閲 |
| `'axes pixels'` | 璺濈杞村煙宸︿笅瑙掔殑鍍忕礌鏁伴噺 |
| `'axes fraction'` | 0,0 鏄酱鍩熷乏涓嬭锛1,1 鏄彸涓婅 |
| `'data'` | 浣跨敤杞村煙鏁版嵁鍧愭爣绯 |
渚嬪灏嗘枃鏈互杞村煙灏忔暟鍧愭爣绯绘潵鏀剧疆锛屾垜浠彲浠ワ細
```py
ax.annotate('local max', xy=(3, 1), xycoords='data',
xytext=(0.8, 0.95), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='right', verticalalignment='top',
)
```
瀵逛簬鐗╃悊鍧愭爣绯伙紙鐐规垨鍍忕礌锛夛紝鍘熺偣鏄浘褰㈡垨杞寸殑宸︿笅瑙掋
鎴栬咃紝浣犲彲浠ラ氳繃鍦ㄥ彲閫夊叧閿瓧鍙傛暟`arrowprops`涓彁渚涚澶村睘鎬у瓧鍏告潵缁樺埗浠庢枃鏈埌娉ㄩ噴鐐圭殑绠ご銆
| `arrowprops`閿 | 鎻忚堪 |
| --- | --- |
| `width` | 绠ご瀹藉害锛屼互鐐逛负鍗曚綅 |
| `frac` | 绠ご澶撮儴鎵鍗犳嵁鐨勬瘮渚 |
| `headwidth` | 绠ご鐨勫簳閮ㄧ殑瀹藉害锛屼互鐐逛负鍗曚綅 |
| `shrink` | 绉诲姩鎻愮ず锛屽苟浣垮叾绂绘敞閲婄偣鍜屾枃鏈竴浜涜窛绂 |
| `**kwargs` | `matplotlib.patches.Polygon`鐨勪换浣曢敭锛屼緥濡`facecolor` |
鍦ㄤ笅闈㈢殑绀轰緥涓紝`xy`鐐规槸鍘熷鍧愭爣锛`xycoords`榛樿涓`'data'`锛夈 瀵逛簬鏋佸潗鏍囪酱锛屽畠鍦`(theta, radius)`绌洪棿涓 姝ょず渚嬩腑鐨勬枃鏈斁缃湪鍥惧舰灏忔暟鍧愭爣绯讳腑銆 `matplotlib.text.Text`鍏抽敭瀛`args`锛屼緥濡`horizontalalignment``verticalalignment``fontsize`锛屼粠`annotate`浼犵粰`Text`瀹炰緥銆
![](http://matplotlib.org/_images/annotation_polar.png)
娉ㄩ噴锛堝寘鎷姳寮忕澶达級鐨勬墍鏈夐珮涓婂ぇ鐨勫唴瀹圭殑鏇村淇℃伅锛岃鍙傞槄[楂樼骇鏍囨敞](http://matplotlib.org/users/annotations.html#plotting-guide-annotation)[`pylab_examples`绀轰緥浠g爜锛歚annotation_demo.py`](http://matplotlib.org/examples/pylab_examples/annotation_demo.html#pylab-examples-annotation-demo)
涓嶈缁х画锛岄櫎闈炰綘宸茬粡闃呰浜[鍩烘湰鏍囨敞](http://matplotlib.org/users/annotations.html#annotations-tutorial)[`text()`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text)[`annotate()`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.annotate)
## 楂樼骇鏍囨敞
### 浣跨敤妗嗗拰鏂囨湰鏉ユ爣娉
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽