gitbook

涓婄骇 cfd2b600
......@@ -8,7 +8,7 @@
matplotlib API 鏈変笁涓眰绾с `matplotlib.backend_bases.FigureCanvas`鏄粯鍒跺浘褰㈢殑鍖哄煙锛`matplotlib.backend_bases.Renderer`鏄煡閬撳浣曞湪`ChartCanvas`涓婄粯鍒剁殑瀵硅薄锛岃`matplotlib.artist.Artist`鏄煡閬撳浣曚娇鐢ㄦ覆鏌撳櫒鍦ㄧ敾甯冧笂鐢诲浘鐨勫璞° `FigureCanvas``Renderer`澶勭悊涓庣敤鎴风晫闈㈠伐鍏峰寘锛堝 wxPython锛夋垨 PostScript庐 绛夌粯鍥捐瑷浜や簰鐨勬墍鏈夌粏鑺傦紝`Artist`澶勭悊鎵鏈夐珮绾х粨鏋勶紝濡傝〃绀哄拰甯冨眬鍥惧舰锛屾枃鏈拰绾挎潯銆傜敤鎴烽氬父瑕佽姳璐95锛呯殑鏃堕棿鏉ュ鐞嗚壓鏈銆
鏈変袱绉嶇被鍨嬬殑鑹烘湳瀹讹細鍩烘湰绫诲瀷鍜屽鍣ㄧ被鍨嬨傚熀鏈被鍨嬭〃绀烘垜浠兂瑕佺粯鍒跺埌鐢诲竷涓婄殑鏍囧噯鍥惧舰瀵硅薄锛`Line2D``Rectangle``Text``AxesImage`绛夛紝瀹瑰櫒鏄斁缃畠浠殑浣嶇疆锛`Axis``Axes``Figure`锛夈傛爣鍑嗙敤娉曟槸鍒涘缓涓涓`Figure`瀹炰緥锛屼娇鐢`Figure`鍒涘缓涓涓垨澶氫釜`Axes``Subplot`瀹炰緥锛屽苟浣跨敤`Axes`瀹炰緥鐨勮緟鍔╂柟娉曟潵鍒涘缓鍩烘湰绫诲瀷銆傚湪涓嬮潰鐨勭ず渚嬩腑锛屾垜浠娇鐢`matplotlib.pyplot.figure()`鍒涘缓涓涓`Figure`瀹炰緥锛岃繖鏄竴涓究鎹风殑鏂规硶锛岀敤浜庡疄渚嬪寲`Figure`瀹炰緥骞跺皢瀹冧滑涓鐨勭敤鎴风晫闈㈡垨缁樺浘宸ュ叿鍖`FigureCanvas`杩炴帴銆傛濡傛垜浠皢鍦ㄤ笅闈㈣璁虹殑锛岃繖涓嶆槸蹇呴』鐨 - 浣犲彲浠ョ洿鎺ヤ娇鐢 PostScript锛孭DF锛孏tk+ 鎴 wxPython `FigureCanvas`瀹炰緥锛岀洿鎺ュ疄渚嬪寲浣犵殑鍥惧舰骞惰繛鎺ュ畠浠 - 浣嗘槸鍥犱负鎴戜滑鍦ㄨ繖閲屽叧娉ㄨ壓鏈 API锛屾垜浠`pyplot`涓烘垜浠鐞嗕竴浜涚粏鑺傦細
鏈変袱绉嶇被鍨嬬殑鑹烘湳瀹讹細鍩烘湰绫诲瀷鍜屽鍣ㄧ被鍨嬨傚熀鏈被鍨嬭〃绀烘垜浠兂瑕佺粯鍒跺埌鐢诲竷涓婄殑鏍囧噯鍥惧舰瀵硅薄锛`Line2D``Rectangle``Text``AxesImage`绛夛紝瀹瑰櫒鏄斁缃畠浠殑浣嶇疆锛`Axis``Axes``Figure`锛夈傛爣鍑嗙敤娉曟槸鍒涘缓涓涓`Figure`瀹炰緥锛屼娇鐢`Figure`鍒涘缓涓涓垨澶氫釜`Axes``Subplot`瀹炰緥锛屽苟浣跨敤`Axes`瀹炰緥鐨勮緟鍔╂柟娉曟潵鍒涘缓鍩烘湰绫诲瀷銆傚湪涓嬮潰鐨勭ず渚嬩腑锛屾垜浠娇鐢`matplotlib.pyplot.figure()`鍒涘缓涓涓`Figure`瀹炰緥锛岃繖鏄竴涓究鎹风殑鏂规硶锛岀敤浜庡疄渚嬪寲`Figure`瀹炰緥骞跺皢瀹冧滑涓鐨勭敤鎴风晫闈㈡垨缁樺浘宸ュ叿鍖`FigureCanvas`杩炴帴銆傛濡傛垜浠皢鍦ㄤ笅闈㈣璁虹殑锛岃繖涓嶆槸蹇呴』鐨 - 浣犲彲浠ョ洿鎺ヤ娇鐢 PostScript锛孭DF锛孏tk+ 鎴 wxPython `FigureCanvas`瀹炰緥锛岀洿鎺ュ疄渚嬪寲浣犵殑鍥惧舰骞惰繛鎺ュ畠浠 - 浣嗘槸鍥犱负鎴戜滑鍦ㄨ繖閲屽叧娉ㄨ壓鏈 API锛屾垜浠`pyplot`涓烘垜浠鐞嗕竴浜涚粏鑺傦細
```py
import matplotlib.pyplot as plt
......@@ -16,7 +16,7 @@ fig = plt.figure()
ax = fig.add_subplot(2,1,1) # two rows, one column, first plot
```
`Axes`鍙兘鏄 matplotlib API 涓渶閲嶈鐨勭被锛屼綘灏嗗湪澶у鏁版椂闂翠娇鐢ㄥ畠銆 杩欐槸鍥犱负`Axes`鏄ぇ澶氭暟瀵硅薄鎵杩涘叆鐨勭粯鍥惧尯鍩燂紝`Axes`鏈夎澶氱壒娈婄殑杈呭姪鏂规硶锛`plot()``text()``hist()``imshow()`锛夋潵鍒涘缓鏈甯歌鐨勫浘褰㈠熀鏈被鍨 `Line2D``Text``Rectangle``Image`锛夈 杩欎簺杈呭姪鏂规硶灏嗚幏鍙鐨勬暟鎹紙渚嬪 numpy 鏁扮粍鍜屽瓧绗︿覆锛夛紝骞舵牴鎹渶瑕佸垱寤哄熀鏈`Artist`瀹炰緥锛堜緥濡傦紝`Line2D`锛夛紝灏嗗畠浠坊鍔犲埌鐩稿叧瀹瑰櫒涓紝骞跺湪璇锋眰鏃剁粯鍒跺畠浠 澶у鏁颁汉鍙兘鐔熸倝瀛愬浘锛岃繖鍙槸`Axes`鐨勪竴涓壒渚嬶紝瀹冨瓨鍦ㄤ簬`Subplot`瀹炰緥鐨勫垪缃戞牸鐨勫浐瀹氳涓娿 濡傛灉瑕佸湪浠绘剰浣嶇疆鍒涘缓`Axes`锛屽彧闇浣跨敤`add_axes()`鏂规硶锛岃鏂规硶鎺ュ彈`[left, bottom, width, height]`鍊肩殑鍒楄〃锛屼互 0~1 鐨勫浘褰㈢浉瀵瑰潗鏍囦负鍗曚綅锛
`Axes`鍙兘鏄 matplotlib API 涓渶閲嶈鐨勭被锛屼綘灏嗗湪澶у鏁版椂闂翠娇鐢ㄥ畠銆 杩欐槸鍥犱负`Axes`鏄ぇ澶氭暟瀵硅薄鎵杩涘叆鐨勭粯鍥惧尯鍩燂紝`Axes`鏈夎澶氱壒娈婄殑杈呭姪鏂规硶锛`plot()``text()``hist()``imshow()`锛夋潵鍒涘缓鏈甯歌鐨勫浘褰㈠熀鏈被鍨 `Line2D``Text``Rectangle``Image`锛夈 杩欎簺杈呭姪鏂规硶灏嗚幏鍙鐨勬暟鎹紙渚嬪 numpy 鏁扮粍鍜屽瓧绗︿覆锛夛紝骞舵牴鎹渶瑕佸垱寤哄熀鏈`Artist`瀹炰緥锛堜緥濡傦紝`Line2D`锛夛紝灏嗗畠浠坊鍔犲埌鐩稿叧瀹瑰櫒涓紝骞跺湪璇锋眰鏃剁粯鍒跺畠浠 澶у鏁颁汉鍙兘鐔熸倝瀛愬浘锛岃繖鍙槸`Axes`鐨勪竴涓壒渚嬶紝瀹冨瓨鍦ㄤ簬`Subplot`瀹炰緥鐨勫垪缃戞牸鐨勫浐瀹氳涓娿 濡傛灉瑕佸湪浠绘剰浣嶇疆鍒涘缓`Axes`锛屽彧闇浣跨敤`add_axes()`鏂规硶锛岃鏂规硶鎺ュ彈`[left, bottom, width, height]`鍊肩殑鍒楄〃锛屼互 0~1 鐨勫浘褰㈢浉瀵瑰潗鏍囦负鍗曚綅锛
```PY
fig2 = plt.figure()
......@@ -32,7 +32,7 @@ s = np.sin(2*np.pi*t)
line, = ax.plot(t, s, color='blue', lw=2)
```
鍦ㄨ繖涓緥瀛愪腑锛`ax`鏄笂闈㈢殑`fig.add_subplot`璋冪敤鍒涘缓鐨`Axes`瀹炰緥锛堣浣`Subplot`鍙槸`Axes`鐨勪竴涓瓙绫伙級锛屽綋浣犺皟鐢`ax.plot`鏃讹紝瀹冨垱寤轰竴涓`Line2D`瀹炰緥骞跺皢鍏舵坊鍔犲埌`Axes.lines`鍒楄〃涓 鍦ㄤ笅闈㈢殑 ipython 浜や簰寮忎細璇濅腑锛鍙互鐪嬪埌`Axes.lines`鍒楄〃鐨勯暱搴︿负 1锛屽苟涓斿寘鍚敱`line, = ax.plot... `璋冪敤杩斿洖鐨勭浉鍚岀嚎鏉★細
鍦ㄨ繖涓緥瀛愪腑锛`ax`鏄笂闈㈢殑`fig.add_subplot`璋冪敤鍒涘缓鐨`Axes`瀹炰緥锛堣浣`Subplot`鍙槸`Axes`鐨勪竴涓瓙绫伙級锛屽綋浣犺皟鐢`ax.plot`鏃讹紝瀹冨垱寤轰竴涓`Line2D`瀹炰緥骞跺皢鍏舵坊鍔犲埌`Axes.lines`鍒楄〃涓 鍦ㄤ笅闈㈢殑 ipython 浜や簰寮忎細璇濅腑锛鍙互鐪嬪埌`Axes.lines`鍒楄〃鐨勯暱搴︿负 1锛屽苟涓斿寘鍚敱`line, = ax.plot... `璋冪敤杩斿洖鐨勭浉鍚岀嚎鏉★細
```py
In [101]: ax.lines[0]
......@@ -42,7 +42,7 @@ In [102]: line
Out[102]: <matplotlib.lines.Line2D instance at 0x19a95710>
```
濡傛灉鎮ㄥ`ax.plot`杩涜杩炵画璋冪敤锛堝苟涓斾繚鎸佺姸鎬佷负鈥渙n鈥濓紝杩欐槸榛樿鍊硷級锛屽垯灏嗗湪鍒楄〃涓坊鍔犲叾浠栫嚎鏉° 鎮鍙互绋嶅悗閫氳繃璋冪敤鍒楄〃鏂规硶鍒犻櫎绾挎潯锛涗换浣曚竴涓柟娉曢兘鍙互锛
濡傛灉浣犲`ax.plot`杩涜杩炵画璋冪敤锛堝苟涓斾繚鎸佺姸鎬佷负銆巓n銆忥紝杩欐槸榛樿鍊硷級锛屽垯灏嗗湪鍒楄〃涓坊鍔犲叾浠栫嚎鏉° 浣鍙互绋嶅悗閫氳繃璋冪敤鍒楄〃鏂规硶鍒犻櫎绾挎潯锛涗换浣曚竴涓柟娉曢兘鍙互锛
```py
del ax.lines[0]
......@@ -64,7 +64,7 @@ ytext = ax.set_ylabel('my ydata')
## 鑷畾涔変綘鐨勫璞
鍥句腑鐨勬瘡涓厓绱犻兘鐢变竴涓 matplotlib 鑹烘湳瀹惰〃绀猴紝姣忎釜鍏冪礌閮芥湁涓涓墿灞曞睘鎬у垪琛ㄧ敤浜庨厤缃畠鐨勫瑙傘 鍥惧舰鏈韩鍖呭惈涓涓`Rectangle`锛屾濂芥槸鍥惧舰鐨勫ぇ灏忥紝浣犲彲浠ヤ娇鐢ㄥ畠鏉ヨ缃浘褰㈢殑鑳屾櫙棰滆壊鍜岄忔槑搴︺ 鍚屾牱锛屾瘡涓`Axes`杈规锛堝湪閫氬父鐨 matplotlib 缁樺浘涓槸鏍囧噯鐨勭櫧搴曢粦杈癸級鎷ユ湁涓涓`Rectangle`瀹炰緥锛岀敤浜庣‘瀹氳酱鍩熺殑棰滆壊锛岄忔槑搴﹀拰鍏朵粬灞炴э紝杩欎簺瀹炰緥瀛樺偍涓烘垚鍛樺彉閲`Figure.patch``Axes.patch`鈥淧atch鈥濇槸涓涓户鎵胯嚜 MATLAB 鐨勫悕绉帮紝瀹冩槸鍥惧舰涓婄殑涓涓鑹茬殑 2D鈥滆ˉ涓佲锛屼緥濡傜煩褰紝鍦嗗拰澶氳竟褰級銆傛瘡涓 matplotlib 鑹烘湳瀹堕兘鏈変互涓嬪睘鎬с
鍥句腑鐨勬瘡涓厓绱犻兘鐢变竴涓 matplotlib 鑹烘湳瀹惰〃绀猴紝姣忎釜鍏冪礌閮芥湁涓涓墿灞曞睘鎬у垪琛ㄧ敤浜庨厤缃畠鐨勫瑙傘 鍥惧舰鏈韩鍖呭惈涓涓`Rectangle`锛屾濂芥槸鍥惧舰鐨勫ぇ灏忥紝浣犲彲浠ヤ娇鐢ㄥ畠鏉ヨ缃浘褰㈢殑鑳屾櫙棰滆壊鍜岄忔槑搴︺ 鍚屾牱锛屾瘡涓`Axes`杈规锛堝湪閫氬父鐨 matplotlib 缁樺浘涓槸鏍囧噯鐨勭櫧搴曢粦杈癸級鎷ユ湁涓涓`Rectangle`瀹炰緥锛岀敤浜庣‘瀹氳酱鍩熺殑棰滆壊锛岄忔槑搴﹀拰鍏朵粬灞炴э紝杩欎簺瀹炰緥瀛樺偍涓烘垚鍛樺彉閲`Figure.patch``Axes.patch`銆嶱atch銆忔槸涓涓户鎵胯嚜 MATLAB 鐨勫悕绉帮紝瀹冩槸鍥惧舰涓婄殑涓涓鑹茬殑 2D銆庤ˉ涓併锛屼緥濡傜煩褰紝鍦嗗拰澶氳竟褰級銆傛瘡涓 matplotlib 鑹烘湳瀹堕兘鏈変互涓嬪睘鎬с
| 灞炴 | 鎻忚堪 |
| --- | --- |
......@@ -127,16 +127,16 @@ In [149]: matplotlib.artist.getp(fig.patch)
zorder = 1
```
鎵鏈夌被鐨勬枃妗e瓧绗︿覆涔熷寘鍚`Artist`灞炴э紝鍥犳鎮ㄥ彲浠ユ煡闃呬氦浜掑紡鈥滃府鍔┾[`Artist`妯″潡](http://matplotlib.org/api/artist_api.html#artist-api)锛屾潵鑾峰彇缁欏畾瀵硅薄鐨勫睘鎬у垪琛ㄣ
鎵鏈夌被鐨勬枃妗e瓧绗︿覆涔熷寘鍚`Artist`灞炴э紝鍥犳浣犲彲浠ユ煡闃呬氦浜掑紡銆庡府鍔┿[`Artist`妯″潡](http://matplotlib.org/api/artist_api.html#artist-api)锛屾潵鑾峰彇缁欏畾瀵硅薄鐨勫睘鎬у垪琛ㄣ
## 瀵硅薄瀹瑰櫒
鐜板湪鎴戜滑鐭ラ亾濡備綍妫鏌ュ拰璁剧疆鎴戜滑鎯宠閰嶇疆鐨勭粰瀹氬璞$殑灞炴э紝鐜板湪鎴戜滑闇瑕佸浣曡幏鍙栬瀵硅薄銆 鍓嶉潰鎻愬埌浜嗕袱绉嶅璞★細鍩烘湰绫诲瀷鍜屽鍣ㄧ被鍨嬨 鍩烘湰绫诲瀷閫氬父鏄綘鎯宠閰嶇疆鐨勪笢瑗匡紙`Text`瀹炰緥鐨勫瓧浣擄紝`Line2D`鐨勫搴︼級锛岃櫧鐒跺鍣ㄤ篃鏈変竴浜涘睘鎬 - 渚嬪 `Axes`鏄竴涓鍣ㄨ壓鏈锛屽寘鍚綘鐨勭粯鍥句腑鐨勮澶氬熀鏈被鍨嬶紝浣嗗畠涔熸湁灞炴э紝姣斿`xscale`鏉ユ帶鍒`xaxis`鈥滅嚎鎬р濊繕鏄滃鏁扳濄 鍦ㄦ湰鑺備腑锛屾垜浠皢鍥為【鍚勭瀹瑰櫒瀵硅薄瀛樺偍鎮鎯宠璁块棶鐨勮壓鏈鐨勪綅缃
鐜板湪鎴戜滑鐭ラ亾濡備綍妫鏌ュ拰璁剧疆鎴戜滑鎯宠閰嶇疆鐨勭粰瀹氬璞$殑灞炴э紝鐜板湪鎴戜滑闇瑕佸浣曡幏鍙栬瀵硅薄銆 鍓嶉潰鎻愬埌浜嗕袱绉嶅璞★細鍩烘湰绫诲瀷鍜屽鍣ㄧ被鍨嬨 鍩烘湰绫诲瀷閫氬父鏄綘鎯宠閰嶇疆鐨勪笢瑗匡紙`Text`瀹炰緥鐨勫瓧浣擄紝`Line2D`鐨勫搴︼級锛岃櫧鐒跺鍣ㄤ篃鏈変竴浜涘睘鎬 - 渚嬪 `Axes`鏄竴涓鍣ㄨ壓鏈锛屽寘鍚綘鐨勭粯鍥句腑鐨勮澶氬熀鏈被鍨嬶紝浣嗗畠涔熸湁灞炴э紝姣斿`xscale`鏉ユ帶鍒`xaxis`銆庣嚎鎬с忚繕鏄庡鏁般忋 鍦ㄦ湰鑺備腑锛屾垜浠皢鍥為【鍚勭瀹瑰櫒瀵硅薄瀛樺偍浣鎯宠璁块棶鐨勮壓鏈鐨勪綅缃
## 鍥惧舰瀹瑰櫒
椤跺眰瀹瑰櫒鑹烘湳瀹舵槸`matplotlib.figure.Figure`锛屽畠鍖呭惈鍥惧舰涓殑鎵鏈夊唴瀹广 鍥惧舰鐨勮儗鏅槸涓涓`Rectangle`锛屽瓨鍌ㄥ湪`Figure.patch`涓 褰鍚戝浘褰腑娣诲姞瀛愬浘锛`add_subplot()`锛夊拰杞村煙锛`add_axes()`锛夋椂锛岃繖浜涗細闄勫姞鍒`Figure.axes`銆 瀹冧滑涔熺敱鍒涘缓瀹冧滑鐨勬柟娉曡繑鍥烇細
椤跺眰瀹瑰櫒鑹烘湳瀹舵槸`matplotlib.figure.Figure`锛屽畠鍖呭惈鍥惧舰涓殑鎵鏈夊唴瀹广 鍥惧舰鐨勮儗鏅槸涓涓`Rectangle`锛屽瓨鍌ㄥ湪`Figure.patch`涓 褰鍚戝浘褰腑娣诲姞瀛愬浘锛`add_subplot()`锛夊拰杞村煙锛`add_axes()`锛夋椂锛岃繖浜涗細闄勫姞鍒`Figure.axes`銆 瀹冧滑涔熺敱鍒涘缓瀹冧滑鐨勬柟娉曡繑鍥烇細
```py
In [156]: fig = plt.figure()
......@@ -152,16 +152,16 @@ In [160]: print fig.axes
[<matplotlib.axes.Subplot instance at 0xd54b26c>, <matplotlib.axes.Axes instance at 0xd3f0b2c>]
```
鍥犱负鍥惧舰缁存姢浜鈥滃綋鍓嶈酱鍩熲濓紙瑙`figure.gca`鍜屽浘`figure.sca`锛夌殑姒傚康浠ユ敮鎸 pylab/pyplot 鐘舵佹満锛屾墍浠ヤ笉搴旂洿鎺ヤ粠杞村煙鍒楄〃涓彃鍏ユ垨鍒犻櫎杞村煙锛岃屽簲浣跨敤`add_subplot()``add_axes()`鏂规硶杩涜鎻掑叆锛屽苟浣跨敤`delaxes()`鏂规硶杩涜鍒犻櫎銆 鐒惰岋紝鎮鍙互鑷敱鍦伴亶鍘嗚酱鍩熷垪琛ㄦ垨绱㈠紩锛屾潵璁块棶瑕佽嚜瀹氫箟鐨`Axes`瀹炰緥銆 涓嬮潰鏄竴涓墦寮鎵鏈夎酱鍩熺綉鏍肩殑绀轰緥锛
鍥犱负鍥惧舰缁存姢浜銆庡綋鍓嶈酱鍩熴忥紙瑙`figure.gca`鍜屽浘`figure.sca`锛夌殑姒傚康浠ユ敮鎸 pylab/pyplot 鐘舵佹満锛屾墍浠ヤ笉搴旂洿鎺ヤ粠杞村煙鍒楄〃涓彃鍏ユ垨鍒犻櫎杞村煙锛岃屽簲浣跨敤`add_subplot()``add_axes()`鏂规硶杩涜鎻掑叆锛屽苟浣跨敤`delaxes()`鏂规硶杩涜鍒犻櫎銆 鐒惰岋紝浣鍙互鑷敱鍦伴亶鍘嗚酱鍩熷垪琛ㄦ垨绱㈠紩锛屾潵璁块棶瑕佽嚜瀹氫箟鐨`Axes`瀹炰緥銆 涓嬮潰鏄竴涓墦寮鎵鏈夎酱鍩熺綉鏍肩殑绀轰緥锛
```py
for ax in fig.axes:
ax.grid(True)
```
鍥惧舰杩樻嫢鏈夎嚜宸辩殑鏂囨湰锛岀嚎鏉★紝琛ヤ竵鍜屽浘鍍忥紝鎮ㄥ彲浠ヤ娇鐢ㄥ畠浠洿鎺ユ坊鍔犲熀鏈被鍨嬨 鍥惧舰鐨勯粯璁ゅ潗鏍囩郴缁熺畝鍗曞湴浠ュ儚绱狅紙杩欓氬父涓嶆槸浣犳兂瑕佺殑锛変负鍗曚綅锛屼絾浣犲彲浠ラ氳繃璁剧疆鎮娣诲姞鍒板浘涓殑鑹烘湳瀹剁殑`transform`灞炴ф潵鎺у埗瀹冦
鍥惧舰杩樻嫢鏈夎嚜宸辩殑鏂囨湰锛岀嚎鏉★紝琛ヤ竵鍜屽浘鍍忥紝浣犲彲浠ヤ娇鐢ㄥ畠浠洿鎺ユ坊鍔犲熀鏈被鍨嬨 鍥惧舰鐨勯粯璁ゅ潗鏍囩郴缁熺畝鍗曞湴浠ュ儚绱狅紙杩欓氬父涓嶆槸浣犳兂瑕佺殑锛変负鍗曚綅锛屼絾浣犲彲浠ラ氳繃璁剧疆浣娣诲姞鍒板浘涓殑鑹烘湳瀹剁殑`transform`灞炴ф潵鎺у埗瀹冦
鏇存湁鐢ㄧ殑鏄鈥滃浘褰㈠潗鏍囩郴鈥濓紝鍏朵腑`(0,0)`鏄浘鐨勫乏涓嬭锛`(1,1)`鏄浘鐨勫彸涓婅锛屾偍鍙互閫氳繃灏`Artist`鐨勫彉鎹㈣缃负`fig.transFigure`鏉ヨ幏寰楋細
鏇存湁鐢ㄧ殑鏄銆庡浘褰㈠潗鏍囩郴銆忥紝鍏朵腑`(0,0)`鏄浘鐨勫乏涓嬭锛`(1,1)`鏄浘鐨勫彸涓婅锛屼綘鍙互閫氳繃灏`Artist`鐨勫彉鎹㈣缃负`fig.transFigure`鏉ヨ幏寰楋細
```py
In [191]: fig = plt.figure()
......@@ -202,7 +202,7 @@ rect = ax.patch # a Rectangle instance
rect.set_facecolor('green')
```
褰撹皟鐢ㄧ粯鍥炬柟娉曪紙渚嬪閫氬父鏄`plot()`锛夊苟浼犻掓暟缁勬垨鍊煎垪琛ㄦ椂锛岃鏂规硶灏嗗垱寤轰竴涓`matplotlib.lines.Line2D()`瀹炰緥锛屽皢鎵鏈`Line2D`灞炴т綔涓哄叧閿瓧鍙傛暟浼犻掞紝 灏嗚绾挎潯娣诲姞鍒`Axes.lines`瀹瑰櫒锛屽苟灏嗗叾杩斿洖缁
褰撹皟鐢ㄧ粯鍥炬柟娉曪紙渚嬪閫氬父鏄`plot()`锛夊苟浼犻掓暟缁勬垨鍊煎垪琛ㄦ椂锛岃鏂规硶灏嗗垱寤轰竴涓`matplotlib.lines.Line2D()`瀹炰緥锛屽皢鎵鏈`Line2D`灞炴т綔涓哄叧閿瓧鍙傛暟浼犻掞紝 灏嗚绾挎潯娣诲姞鍒`Axes.lines`瀹瑰櫒锛屽苟灏嗗叾杩斿洖缁
```py
In [213]: x, y = np.random.rand(2, 100)
......@@ -228,7 +228,7 @@ Out[234]: <a list of 50 Patch objects>
In [235]: print len(ax.patches)
```
浣犱笉搴旇鐩存帴灏嗗璞℃坊鍔犲埌`Axes.lines``Axes.patches`鍒楄〃锛岄櫎闈炰綘纭垏鐭ラ亾浣犲湪鍋氫粈涔堬紝鍥犱负`Axes`闇瑕佸湪瀹冨垱寤哄拰娣诲姞瀵硅薄鍋氫竴浜涗簨鎯呫 瀹冭缃`Artist``figure``axes`灞炴э紝浠ュ強榛樿`Axes`鍙樻崲锛堥櫎闈炶缃簡鍙樻崲锛夈 瀹冭繕妫鏌`Artist`涓寘鍚殑鏁版嵁锛屾潵鏇存柊鎺у埗鑷姩缂╂斁鐨勬暟鎹粨鏋勶紝浠ヤ究鍙互璋冩暣瑙嗗浘闄愬埗鏉ュ寘鍚粯鍒剁殑鏁版嵁銆 浣嗘槸锛鍙互鑷繁鍒涘缓瀵硅薄锛屽苟浣跨敤杈呭姪鏂规硶锛堝`add_line()``add_patch()`锛夊皢瀹冧滑鐩存帴娣诲姞鍒`Axes`銆 杩欓噷鏄竴涓敞閲婄殑浜や簰寮忎細璇濓紝璇存槑姝e湪鍙戠敓浠涔堬細
浣犱笉搴旇鐩存帴灏嗗璞℃坊鍔犲埌`Axes.lines``Axes.patches`鍒楄〃锛岄櫎闈炰綘纭垏鐭ラ亾浣犲湪鍋氫粈涔堬紝鍥犱负`Axes`闇瑕佸湪瀹冨垱寤哄拰娣诲姞瀵硅薄鍋氫竴浜涗簨鎯呫 瀹冭缃`Artist``figure``axes`灞炴э紝浠ュ強榛樿`Axes`鍙樻崲锛堥櫎闈炶缃簡鍙樻崲锛夈 瀹冭繕妫鏌`Artist`涓寘鍚殑鏁版嵁锛屾潵鏇存柊鎺у埗鑷姩缂╂斁鐨勬暟鎹粨鏋勶紝浠ヤ究鍙互璋冩暣瑙嗗浘闄愬埗鏉ュ寘鍚粯鍒剁殑鏁版嵁銆 浣嗘槸锛鍙互鑷繁鍒涘缓瀵硅薄锛屽苟浣跨敤杈呭姪鏂规硶锛堝`add_line()``add_patch()`锛夊皢瀹冧滑鐩存帴娣诲姞鍒`Axes`銆 杩欓噷鏄竴涓敞閲婄殑浜や簰寮忎細璇濓紝璇存槑姝e湪鍙戠敓浠涔堬細
```py
In [261]: fig = plt.figure()
......@@ -297,7 +297,7 @@ In [274]: ax.figure.canvas.draw()
| `ax.scatter` - 鏁g偣鍥 | `PolygonCollection` | `ax.collections` |
| `ax.text` - 鏂囨湰 | `Text` | `ax.texts` |
闄や簡鎵鏈夎繖浜涜壓鏈锛`Axes`鍖呭惈涓や釜閲嶈鐨勮壓鏈瀹瑰櫒锛`XAxis``YAxis`锛屽畠浠鐞嗗埢搴﹀拰鏍囩鐨勭粯鍒躲 瀹冧滑琚瓨鍌ㄤ负瀹炰緥鍙橀噺`xaxis``yaxis``XAxis``YAxis`瀹瑰櫒灏嗗湪涓嬮潰璇︾粏浠嬬粛锛屼絾璇锋敞鎰忥紝`Axes`鍖呭惈璁稿杈呭姪鏂规硶锛屽畠浠細灏嗚皟鐢ㄨ浆鍙戠粰`Axis`瀹炰緥锛屽洜姝鎮ㄩ氬父涓嶉渶瑕佺洿鎺ヤ娇鐢ㄥ畠浠紝闄ら潪鎮ㄦ効鎰忋 渚嬪锛屾偍鍙互浣跨敤`Axes`杈呭姪绋嬪簭鏂规硶璁剧疆`XAxis`鍒诲害鏍囩鐨勫瓧浣撳ぇ灏忥細
闄や簡鎵鏈夎繖浜涜壓鏈锛`Axes`鍖呭惈涓や釜閲嶈鐨勮壓鏈瀹瑰櫒锛`XAxis``YAxis`锛屽畠浠鐞嗗埢搴﹀拰鏍囩鐨勭粯鍒躲 瀹冧滑琚瓨鍌ㄤ负瀹炰緥鍙橀噺`xaxis``yaxis``XAxis``YAxis`瀹瑰櫒灏嗗湪涓嬮潰璇︾粏浠嬬粛锛屼絾璇锋敞鎰忥紝`Axes`鍖呭惈璁稿杈呭姪鏂规硶锛屽畠浠細灏嗚皟鐢ㄨ浆鍙戠粰`Axis`瀹炰緥锛屽洜姝浣犻氬父涓嶉渶瑕佺洿鎺ヤ娇鐢ㄥ畠浠紝闄ら潪浣犳効鎰忋 渚嬪锛屼綘鍙互浣跨敤`Axes`杈呭姪绋嬪簭鏂规硶璁剧疆`XAxis`鍒诲害鏍囩鐨勫瓧浣撳ぇ灏忥細
```py
for label in ax.get_xticklabels():
......@@ -321,9 +321,9 @@ for label in ax.get_xticklabels():
## 杞村鍣
`matplotlib.axis.Axis`瀹炰緥澶勭悊鍒诲害绾匡紝缃戞牸绾匡紝鍒诲害鏍囩鍜岃酱鏍囩鐨勭粯鍒躲鍙互鍒嗗埆涓簓杞撮厤缃乏鍜屽彸鍒诲害锛屼负x杞村垎鍒厤缃笂鍜屼笅鍒诲害銆 `Axis`杩樺瓨鍌ㄥ湪鑷姩缂╂斁锛屽钩绉诲拰缂╂斁涓娇鐢ㄧ殑鏁版嵁鍜岃鍥鹃棿闅旓紝浠ュ強`Locator``Formatter`瀹炰緥锛屽畠浠帶鍒跺埢搴︿綅缃互鍙婂畠浠〃绀轰负瀛楃涓茬殑鏂瑰紡銆
`matplotlib.axis.Axis`瀹炰緥澶勭悊鍒诲害绾匡紝缃戞牸绾匡紝鍒诲害鏍囩鍜岃酱鏍囩鐨勭粯鍒躲鍙互鍒嗗埆涓簓杞撮厤缃乏鍜屽彸鍒诲害锛屼负x杞村垎鍒厤缃笂鍜屼笅鍒诲害銆 `Axis`杩樺瓨鍌ㄥ湪鑷姩缂╂斁锛屽钩绉诲拰缂╂斁涓娇鐢ㄧ殑鏁版嵁鍜岃鍥鹃棿闅旓紝浠ュ強`Locator``Formatter`瀹炰緥锛屽畠浠帶鍒跺埢搴︿綅缃互鍙婂畠浠〃绀轰负瀛楃涓茬殑鏂瑰紡銆
姣忎釜`Axis`瀵硅薄閮藉寘鍚竴涓`label`灞炴э紙杩欐槸 pylab 鍦ㄨ皟鐢`xlabel()``ylabel()`鏃朵慨鏀圭殑涓滆タ锛変互鍙婁富鍜屾鍒诲害鐨勫垪琛ㄣ傚埢搴︽槸`XTick``YTick`瀹炰緥锛屽畠鍖呭惈娓叉煋鍒诲害鍜屽埢搴︽爣绛剧殑瀹為檯绾挎潯鍜屾枃鏈熀鏈被鍨嬨傚洜涓哄埢搴︽槸鎸夐渶鍔ㄦ佸垱寤虹殑锛堜緥濡傦紝褰撳钩绉诲拰缂╂斁鏃讹級锛搴旇閫氳繃璁块棶鍣ㄦ柟娉`get_major_ticks()``get_minor_ticks()`璁块棶涓诲拰娆″埢搴︾殑鍒楄〃銆傝櫧鐒跺埢搴﹀寘鍚墍鏈変笅闈㈣鎻愬強鐨勫熀鏈被鍨嬶紝`Axis`鏂规硶鍖呭惈璁块棶鍣ㄦ柟娉曟潵杩斿洖鍒诲害绾匡紝鍒诲害鏍囩锛屽埢搴︿綅缃瓑锛
姣忎釜`Axis`瀵硅薄閮藉寘鍚竴涓`label`灞炴э紙杩欐槸 pylab 鍦ㄨ皟鐢`xlabel()``ylabel()`鏃朵慨鏀圭殑涓滆タ锛変互鍙婁富鍜屾鍒诲害鐨勫垪琛ㄣ傚埢搴︽槸`XTick``YTick`瀹炰緥锛屽畠鍖呭惈娓叉煋鍒诲害鍜屽埢搴︽爣绛剧殑瀹為檯绾挎潯鍜屾枃鏈熀鏈被鍨嬨傚洜涓哄埢搴︽槸鎸夐渶鍔ㄦ佸垱寤虹殑锛堜緥濡傦紝褰撳钩绉诲拰缂╂斁鏃讹級锛搴旇閫氳繃璁块棶鍣ㄦ柟娉`get_major_ticks()``get_minor_ticks()`璁块棶涓诲拰娆″埢搴︾殑鍒楄〃銆傝櫧鐒跺埢搴﹀寘鍚墍鏈変笅闈㈣鎻愬強鐨勫熀鏈被鍨嬶紝`Axis`鏂规硶鍖呭惈璁块棶鍣ㄦ柟娉曟潵杩斿洖鍒诲害绾匡紝鍒诲害鏍囩锛屽埢搴︿綅缃瓑锛
```py
In [285]: axis = ax.xaxis
......
# Matplotlib 鐢ㄦ埛鎸囧崡
> 鏉ユ簮锛歔User's Guide](http://matplotlib.org/users/index.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
+ [鍦ㄧ嚎闃呰](https://www.gitbook.com/book/wizardforcel/matplotlib-user-guide/details)
+ [PDF鏍煎紡](https://www.gitbook.com/download/pdf/book/wizardforcel/matplotlib-user-guide)
+ [EPUB鏍煎紡](https://www.gitbook.com/download/epub/book/wizardforcel/matplotlib-user-guide)
+ [MOBI鏍煎紡](https://www.gitbook.com/download/mobi/book/wizardforcel/matplotlib-user-guide)
+ [浠g爜浠撳簱](https://github.com/wizardforcel/matplotlib-user-guide-zh)
## 璧炲姪鎴
![](http://upload-images.jianshu.io/upload_images/118142-fe132ca3591a3d52.png)
\ No newline at end of file
## 鏈琛
| 鑻辨枃 | 涓枃 |
| --- | --- |
| Annotation | 鏍囨敞 |
| Artist | 鑹烘湳瀹 |
| Axes | 杞村煙 |
| Axis | 杞/鍧愭爣杞 |
| B茅zier | 璐濆灏 |
| Coordinate | 鍧愭爣 |
| Coordinate System | 鍧愭爣绯 |
| Figure | 鍥惧舰 |
| Handle | 鍙ユ焺 |
| Handler | 澶勭悊鍣 |
| Image | 鍥惧儚 |
| Legend | 鍥句緥 |
| Line | 绾挎潯 |
| Patch | 琛ヤ竵 |
| Path | 璺緞 |
| Pick | 鎷惧彇 |
| Subplot | 瀛愬浘 |
| Text | 鏂囨湰 |
| Tick | 鍒诲害 |
| Tick Label | 鍒诲害鏍囩 |
| Transformation | 鍙樻崲 |
/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery - http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */
/* RESET
=============================================================================*/
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
}
/* BODY
=============================================================================*/
body {
font-family: Helvetica, arial, freesans, clean, sans-serif;
font-size: 14px;
line-height: 1.6;
color: #333;
background-color: #fff;
padding: 20px;
max-width: 960px;
margin: 0 auto;
}
body>*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h2 {
font-size: 18px;
color: #000;
}
h3 {
font-size: 14px;
}
h4 {
font-size: 14px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽