3.5.

涓婄骇 9bd25b30
# Artist 鏁欑▼
matplotlib API 鏈変笁涓眰绾с `matplotlib.backend_bases.FigureCanvas`鏄粯鍒跺浘褰㈢殑鍖哄煙锛`matplotlib.backend_bases.Renderer`鏄煡閬撳浣曞湪`ChartCanvas`涓婄粯鍒剁殑瀵硅薄锛岃`matplotlib.artist.Artist`鏄煡閬撳浣曚娇鐢ㄦ覆鏌撳櫒鍦ㄧ敾甯冧笂鐢诲浘鐨勫璞° `FigureCanvas``Renderer`澶勭悊涓庣敤鎴风晫闈㈠伐鍏峰寘锛堝 wxPython锛夋垨 PostScript庐 绛夌粯鍥捐瑷浜や簰鐨勬墍鏈夌粏鑺傦紝`Artist`澶勭悊鎵鏈夐珮绾х粨鏋勶紝濡傝〃绀哄拰甯冨眬鍥惧舰锛屾枃鏈拰绾挎潯銆傞氬父鐢ㄦ埛璇濊垂95锛呯殑鏃堕棿鏉ュ鐞 Artist銆
鏈変袱绉嶇被鍨嬬殑 Artist锛氬熀鏈被鍨嬪拰瀹瑰櫒绫诲瀷銆傚熀鏈被鍨嬭〃绀烘垜浠兂瑕佺粯鍒跺埌鐢诲竷涓婄殑鏍囧噯鍥惧舰瀵硅薄锛`Line2D``Rectangle``Text``AxesImage`绛夛紝瀹瑰櫒鏄斁缃畠浠殑浣嶇疆锛`Axis``Axes``Figure`锛夈傛爣鍑嗙敤娉曟槸鍒涘缓涓涓`Figure`瀹炰緥锛屼娇鐢`Figure`鍒涘缓涓涓垨澶氫釜`Axes``Subplot`瀹炰緥锛屽苟浣跨敤`Axes`瀹炰緥鐨勮緟鍔╂柟娉曟潵鍒涘缓鍩烘湰绫诲瀷銆傚湪涓嬮潰鐨勭ず渚嬩腑锛屾垜浠娇鐢`matplotlib.pyplot.figure()`鍒涘缓涓涓`Figure`瀹炰緥锛岃繖鏄竴涓究鎹风殑鏂规硶锛岀敤浜庡疄渚嬪寲`Figure`瀹炰緥骞跺皢瀹冧滑涓庢偍鐨勭敤鎴风晫闈㈡垨缁樺浘宸ュ叿鍖`FigureCanvas`杩炴帴銆傛濡傛垜浠皢鍦ㄤ笅闈㈣璁虹殑锛岃繖涓嶆槸蹇呴』鐨 - 浣犲彲浠ョ洿鎺ヤ娇鐢 PostScript锛孭DF锛孏tk+ 鎴 wxPython `FigureCanvas`瀹炰緥锛岀洿鎺ュ疄渚嬪寲浣犵殑鍥惧舰骞惰繛鎺ュ畠浠 - 浣嗘槸鍥犱负鎴戜滑鍦ㄨ繖閲屽叧娉 Artist API锛屾垜浠`pyplot`涓烘垜浠鐞嗕竴浜涚粏鑺傦細
```py
import matplotlib.pyplot as plt
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 鐨勫浘褰㈢浉瀵瑰潗鏍囦负鍗曚綅锛
```PY
fig2 = plt.figure()
ax2 = fig2.add_axes([0.15, 0.1, 0.7, 0.3])
```
浠ユ垜浠殑渚嬪瓙缁х画锛
```py
import numpy as np
t = np.arange(0.0, 1.0, 0.01)
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... `璋冪敤杩斿洖鐨勭浉鍚岀嚎鏉★細
```py
In [101]: ax.lines[0]
Out[101]: <matplotlib.lines.Line2D instance at 0x19a95710>
In [102]: line
Out[102]: <matplotlib.lines.Line2D instance at 0x19a95710>
```
濡傛灉鎮ㄥ`ax.plot`杩涜杩炵画璋冪敤锛堝苟涓斾繚鎸佺姸鎬佷负鈥渙n鈥濓紝杩欐槸榛樿鍊硷級锛屽垯灏嗗湪鍒楄〃涓坊鍔犲叾浠栫嚎鏉° 鎮ㄥ彲浠ョ◢鍚庨氳繃璋冪敤鍒楄〃鏂规硶鍒犻櫎绾挎潯锛涗换浣曚竴涓柟娉曢兘鍙互锛
```py
del ax.lines[0]
ax.lines.remove(line) # one or the other, not both!
```
杞村煙涔熸嫢鏈夎緟鍔╂柟娉曪紝鐢ㄤ簬璁剧疆鍜岃楗 x 鍜 y 杞寸殑鍒诲害銆佸埢搴︽爣绛惧拰杞存爣绛撅細
```py
xtext = ax.set_xlabel('my xdata') # returns a Text instance
ytext = ax.set_ylabel('my ydata')
```
褰撲綘璋冪敤`ax.set_xlabel`鏃讹紝瀹冨皢淇℃伅浼犻掔粰`XAxis``Text`瀹炰緥锛屾瘡涓`Axes `瀹炰緥閮藉寘鍚`XAxis``YAxis`锛屽畠浠鐞嗗埢搴︺佸埢搴︽爣绛惧拰杞存爣绛剧殑甯冨眬鍜岀粯鍒躲
灏濊瘯鍒涘缓涓嬮潰鐨勫浘褰細
![](http://matplotlib.org/_images/fig_axes_labels_simple.png)
## 鑷畾涔変綘鐨勫璞
鍥句腑鐨勬瘡涓厓绱犻兘鐢变竴涓 matplotlib Artist 琛ㄧず锛屾瘡涓厓绱犻兘鏈変竴涓墿灞曞睘鎬у垪琛ㄧ敤浜庨厤缃畠鐨勫瑙傘 鍥惧舰鏈韩鍖呭惈涓涓`Rectangle`锛屾濂芥槸鍥惧舰鐨勫ぇ灏忥紝浣犲彲浠ヤ娇鐢ㄥ畠鏉ヨ缃浘褰㈢殑鑳屾櫙棰滆壊鍜岄忔槑搴︺ 鍚屾牱锛屾瘡涓`Axes`杈规锛堝湪閫氬父鐨 matplotlib 缁樺浘涓槸鏍囧噯鐨勭櫧搴曢粦杈癸級鎷ユ湁涓涓`Rectangle`瀹炰緥锛岀敤浜庣‘瀹氳酱鍩熺殑棰滆壊锛岄忔槑搴﹀拰鍏朵粬灞炴э紝杩欎簺瀹炰緥瀛樺偍涓烘垚鍛樺彉閲`Figure.patch``Axes.patch`锛堚淧atch鈥濇槸涓涓户鎵胯嚜 MATLAB 鐨勫悕绉帮紝瀹冩槸鍥惧舰涓婄殑涓涓鑹茬殑 2D鈥滆ˉ涓佲濓紝渚嬪鐭╁舰锛屽渾鍜屽杈瑰舰锛夈傛瘡涓 matplotlib Artist 閮芥湁浠ヤ笅灞炴с
| 灞炴 | 鎻忚堪 |
| --- | --- |
| alpha | 閫忔槑搴 - 0 ~ 1 鐨勬爣閲 |
| animated | 鐢ㄤ簬甯姪鍔ㄧ敾缁樺埗鐨勫竷灏斿 |
| axes | Artist 鎵鍦ㄧ殑杞村煙锛屽彲鑳戒负绌 |
| clip_box | 鐢ㄤ簬鍓垏 Artist 鐨勮竟妗 |
| clip_on | 鍓垏鏄惁寮鍚 |
| clip_path | Artist 琚壀鍒囩殑璺緞 |
| contains | 涓涓嬀鍙栧嚱鏁帮紝鐢ㄤ簬鍒ゆ柇 Artist 鏄惁浣嶄簬鎷惧彇鐐 |
| figure | Artist 鎵鍦ㄧ殑鍥惧舰瀹炰緥锛屽彲鑳戒负绌 |
| label | 鏂囨湰鏍囩锛堢敤浜庤嚜鍔ㄦ爣璁帮級 |
| picker | 鎺у埗瀵硅薄鎷惧彇鐨 Python 瀵硅薄 |
| transform | 鍙樻崲 |
| visible | 甯冨皵鍊硷紝琛ㄧず Artist 鏄惁搴旇缁樺埗 |
| zorder | 纭畾缁樺埗椤哄簭鐨勬暟鍊 |
| rasterized | 甯冨皵鍊硷紝鏄惁灏嗗悜閲忚浆鎹负鍏夋爡鍥惧舰锛堝嚭浜庡帇缂╂垨 eps 閫忔槑搴︼級 |
姣忎釜灞炴ч兘浣跨敤涓涓佸紡鐨`setter``getter`锛堟槸鐨勶紝鎴戜滑鐭ラ亾杩欎細鍒烘縺 Python 鐖卞ソ鑰咃紝鎴戜滑璁″垝鏀寔閫氳繃灞炴ф垨 traits 鐩存帴璁块棶锛屼絾瀹冭繕娌℃湁瀹屾垚锛夈 渚嬪锛岃灏嗗綋鍓`alpha`鍊煎彉涓轰竴鍗婏細
```py
a = o.get_alpha()
o.set_alpha(0.5*a)
```
濡傛灉浣犳墦绠楀彲浠ヤ竴娆℃ц缃竴浜涘睘鎬э紝浣犱篃鍙互浠ュ叧閿瓧鍙傛暟浣跨敤`set`鏂规硶锛屼緥濡傦細
```py
o.set(alpha=0.5, zorder=2)
```
濡傛灉浣犲湪 Python 浜や簰寮 Shell 涓伐浣滐紝妫鏌`Artist`灞炴х殑涓绉嶆柟渚跨殑鏂规硶鏄娇鐢`matplotlib.artist.getp()`鍑芥暟锛堝湪 pylab 涓彧闇瑕`getp()`锛夛紝瀹冨垪鍑轰簡灞炴у強鍏跺笺 杩欓傜敤浜庝粠`Artist`娲剧敓鐨勭被锛屼緥濡`Figure``Rectangle`銆 杩欓噷鏄笂闈㈡彁鍒扮殑`Figure`鐨勭煩褰㈠睘鎬э細
```py
In [149]: matplotlib.artist.getp(fig.patch)
alpha = 1.0
animated = False
antialiased or aa = True
axes = None
clip_box = None
clip_on = False
clip_path = None
contains = None
edgecolor or ec = w
facecolor or fc = 0.75
figure = Figure(8.125x6.125)
fill = 1
hatch = None
height = 1
label =
linewidth or lw = 1.0
picker = None
transform = <Affine object at 0x134cca84>
verts = ((0, 0), (0, 1), (1, 1), (1, 0))
visible = True
width = 1
window_extent = <Bbox object at 0x134acbcc>
x = 0
y = 0
zorder = 1
```
鎵鏈夌被鐨勬枃妗e瓧绗︿覆涔熷寘鍚`Artist`灞炴э紝鍥犳鎮ㄥ彲浠ユ煡闃呬氦浜掑紡鈥滃府鍔┾濇垨 [Artist 妯″潡](http://matplotlib.org/api/artist_api.html#artist-api)锛屾潵鑾峰彇缁欏畾瀵硅薄鐨勫睘鎬у垪琛ㄣ
## 瀵硅薄瀹瑰櫒
鐜板湪鎴戜滑鐭ラ亾濡備綍妫鏌ュ拰璁剧疆鎴戜滑鎯宠閰嶇疆鐨勭粰瀹氬璞$殑灞炴э紝鐜板湪鎴戜滑闇瑕佸浣曡幏鍙栬瀵硅薄銆 鍓嶉潰鎻愬埌浜嗕袱绉嶅璞★細鍩烘湰绫诲瀷鍜屽鍣ㄧ被鍨嬨 鍩烘湰绫诲瀷閫氬父鏄綘鎯宠閰嶇疆鐨勪笢瑗匡紙`Text`瀹炰緥鐨勫瓧浣擄紝`Line2D`鐨勫搴︼級锛岃櫧鐒跺鍣ㄤ篃鏈変竴浜涘睘鎬 - 渚嬪 `Axes`鏄竴涓鍣 Artist锛屽寘鍚綘鐨勭粯鍥句腑鐨勮澶氬熀鏈被鍨嬶紝浣嗗畠涔熸湁灞炴э紝姣斿`xscale`鏉ユ帶鍒`xaxis`鏄滅嚎鎬р濊繕鏄滃鏁扳濄 鍦ㄦ湰鑺備腑锛屾垜浠皢鍥為【鍚勭瀹瑰櫒瀵硅薄瀛樺偍鎮ㄦ兂瑕佽闂殑 Artist 鐨勪綅缃
## 鍥惧舰瀹瑰櫒
椤跺眰瀹瑰櫒 Artist 鏄`matplotlib.figure.Figure`锛屽畠鍖呭惈鍥惧舰涓殑鎵鏈夊唴瀹广 鍥惧舰鐨勮儗鏅槸涓涓`Rectangle`锛屽瓨鍌ㄥ湪`Figure.patch`涓 褰撴偍鍚戝浘褰腑娣诲姞瀛愬浘锛`add_subplot()`锛夊拰杞村煙锛`add_axes()`锛夋椂锛岃繖浜涗細闄勫姞鍒`Figure.axes`銆 瀹冧滑涔熺敱鍒涘缓瀹冧滑鐨勬柟娉曡繑鍥烇細
```py
In [156]: fig = plt.figure()
In [157]: ax1 = fig.add_subplot(211)
In [158]: ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.3])
In [159]: ax1
Out[159]: <matplotlib.axes.Subplot instance at 0xd54b26c>
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`瀹炰緥銆 涓嬮潰鏄竴涓墦寮鎵鏈夎酱鍩熺綉鏍肩殑绀轰緥锛
```py
for ax in fig.axes:
ax.grid(True)
```
鍥惧舰杩樻嫢鏈夎嚜宸辩殑鏂囨湰锛岀嚎鏉★紝琛ヤ竵鍜屽浘鍍忥紝鎮ㄥ彲浠ヤ娇鐢ㄥ畠浠洿鎺ユ坊鍔犲熀鏈被鍨嬨 鍥惧舰鐨勯粯璁ゅ潗鏍囩郴缁熺畝鍗曞湴浠ュ儚绱狅紙杩欓氬父涓嶆槸浣犳兂瑕佺殑锛変负鍗曚綅锛屼絾浣犲彲浠ラ氳繃璁剧疆鎮ㄦ坊鍔犲埌鍥句腑鐨 Artist 鐨`transform`灞炴ф潵鎺у埗瀹冦
鏇存湁鐢ㄧ殑鏄滃浘褰㈠潗鏍囩郴鈥濓紝鍏朵腑`(0,0)`鏄浘鐨勫乏涓嬭锛`(1,1)`鏄浘鐨勫彸涓婅锛屾偍鍙互閫氳繃灏`Artist`鐨勮浆鎹㈣缃负`fig.transFigure`鏉ヨ幏寰楋細
```py
In [191]: fig = plt.figure()
In [192]: l1 = matplotlib.lines.Line2D([0, 1], [0, 1],
transform=fig.transFigure, figure=fig)
In [193]: l2 = matplotlib.lines.Line2D([0, 1], [1, 0],
transform=fig.transFigure, figure=fig)
In [194]: fig.lines.extend([l1, l2])
In [195]: fig.canvas.draw()
```
![](http://matplotlib.org/_images/fig_x.png)
杩欓噷鏄浘褰㈠彲浠ュ寘鍚殑鑹烘湳瀹舵荤粨锛
| 鍥惧舰灞炴 | 鎻忚堪 |
| --- | --- |
| `axes` | `Axes`瀹炰緥鐨勫垪琛紙鍖呮嫭`Subplot`锛 |
| `patch` | `Rectangle`鑳屾櫙 |
| `images` | `FigureImages`琛ヤ竵鐨勫垪琛 - 鐢ㄤ簬鍘熷鍍忕礌鏄剧ず |
| `legends` | 鍥惧舰`Legend`瀹炰緥鐨勫垪琛紙涓嶅悓浜`Axes.legends`锛 |
| `lines` | 鍥惧舰`Line2D`瀹炰緥鐨勫垪琛紙寰堝皯浣跨敤锛岃`Axes.lines`锛 |
| `patches` | 鍥惧舰琛ヤ竵鍒楄〃锛堝緢灏戜娇鐢紝瑙`Axes.patches`锛 |
| `texts` | 鍥惧舰`Text`瀹炰緥鐨勫垪琛 |
## 杞村煙瀹瑰櫒
`matplotlib.axes.Axes`鏄 matplotlib 瀹囧畽鐨勪腑蹇 - 瀹冨寘鍚粷澶у鏁板湪涓涓浘褰腑浣跨敤鐨勮壓鏈锛屽苟甯︽湁璁稿杈呭姪鏂规硶鏉ュ垱寤哄拰娣诲姞杩欎簺鑹烘湳瀹舵湰韬紝浠ュ強璁块棶鍜岃嚜瀹氫箟鎵鍖呭惈鐨勮壓鏈鐨勮緟鍔╂柟娉曘 灏卞儚`Figure`閭f牱锛屽畠鍖呭惈涓涓`Patch patch`锛屽畠鏄竴涓敤浜庣瑳鍗″皵鍧愭爣鐨`Rectangle`鍜屼竴涓敤浜庢瀬鍧愭爣鐨`Cirecle`; 杩欎釜琛ヤ竵鍐冲畾浜嗙粯鍥惧尯鍩熺殑褰㈢姸锛岃儗鏅拰杈规锛
```py
ax = fig.add_subplot(111)
rect = ax.patch # a Rectangle instance
rect.set_facecolor('green')
```
褰撹皟鐢ㄧ粯鍥炬柟娉曪紙渚嬪閫氬父鏄`plot()`锛夊苟浼犻掓暟缁勬垨鍊煎垪琛ㄦ椂锛岃鏂规硶灏嗗垱寤轰竴涓`matplotlib.lines.Line2D()`瀹炰緥锛屽皢鎵鏈`Line2D`灞炴т綔涓哄叧閿瓧鍙傛暟浼犻掞紝 灏嗚绾挎潯娣诲姞鍒`Axes.lines`瀹瑰櫒锛屽苟灏嗗叾杩斿洖缁欐偍锛
```py
In [213]: x, y = np.random.rand(2, 100)
In [214]: line, = ax.plot(x, y, '-', color='blue', linewidth=2)
```
`plot`杩斿洖涓涓嚎鏉″垪琛紝鍥犱负浣犲彲浠ヤ紶鍏ュ涓`x,y`鍋跺鏉ョ粯鍒讹紝鎴戜滑灏嗛暱搴︿负涓鐨勫垪琛ㄧ殑绗竴涓厓绱犺В鏋勫埌`line`鍙橀噺涓 璇ョ嚎鏉″凡娣诲姞鍒`Axes.lines`鍒楄〃涓細
```py
In [229]: print ax.lines
[<matplotlib.lines.Line2D instance at 0xd378b0c>]
```
涓庝箣绫讳技锛屽垱寤鸿ˉ涓佺殑鏂规硶锛堝`bar()`锛変細鍒涘缓涓涓煩褰㈠垪琛紝灏嗚ˉ涓佹坊鍔犲埌`Axes.patches`鍒楄〃涓細
```py
In [233]: n, bins, rectangles = ax.hist(np.random.randn(1000), 50, facecolor='yellow')
In [234]: rectangles
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湪鍙戠敓浠涔堬細
```py
In [261]: fig = plt.figure()
In [262]: ax = fig.add_subplot(111)
# create a rectangle instance
In [263]: rect = matplotlib.patches.Rectangle( (1,1), width=5, height=12)
# by default the axes instance is None
In [264]: print rect.get_axes()
None
# and the transformation instance is set to the "identity transform"
In [265]: print rect.get_transform()
<Affine object at 0x13695544>
# now we add the Rectangle to the Axes
In [266]: ax.add_patch(rect)
# and notice that the ax.add_patch method has set the axes
# instance
In [267]: print rect.get_axes()
Axes(0.125,0.1;0.775x0.8)
# and the transformation has been set too
In [268]: print rect.get_transform()
<Affine object at 0x15009ca4>
# the default axes transformation is ax.transData
In [269]: print ax.transData
<Affine object at 0x15009ca4>
# notice that the xlimits of the Axes have not been changed
In [270]: print ax.get_xlim()
(0.0, 1.0)
# but the data limits have been updated to encompass the rectangle
In [271]: print ax.dataLim.bounds
(1.0, 1.0, 5.0, 12.0)
# we can manually invoke the auto-scaling machinery
In [272]: ax.autoscale_view()
# and now the xlim are updated to encompass the rectangle
In [273]: print ax.get_xlim()
(1.0, 6.0)
# we have to manually force a figure draw
In [274]: ax.figure.canvas.draw()
```
鏈夐潪甯稿鐨`Axes`杈呭姪鏂规硶鐢ㄤ簬鍒涘缓鍩烘湰鑹烘湳瀹跺苟灏嗗畠浠坊鍔犲埌浠栦滑鍚勮嚜鐨勫鍣ㄤ腑銆 涓嬭〃鎬荤粨浜嗕粬浠殑涓閮ㄥ垎锛屼粬浠垱閫犵殑`Artist`鐨勭绫伙紝浠ュ強浠栦滑鍦ㄥ摢閲屽瓨鍌ㄥ畠浠
| 杈呭姪鏂规硶 | 鑹烘湳瀹 | 瀹瑰櫒 |
| --- | --- | --- |
| `ax.annotate` - 鏂囨湰鏍囨敞 | `Annotate` | `ax.texts` |
| `ax.bar` - 鏉″舰鍥 | `Rectangle` | `ax.patches` |
| `ax.errorbar` - 璇樊鏉″舰鍥 | `Line2D``Rectangle` | `ax.lines``ax.patches` |
| `ax.fill` - 鍏变韩鍖哄煙 | `Polygon` | `ax.patches` |
| `ax.hist` - 鐩存柟鍥 | `Rectangle` | `ax.patches` |
| `ax.imshow` - 鍥惧儚鏁版嵁 | `AxesImage` | `ax.images` |
| `ax.legend` - 杞村煙鍥句緥 | `Legend` | `ax.legends` |
| `ax.plot` - xy 缁樺浘 | `Line2D` | `ax.lines` |
| `ax.scatter` - 鏁g偣鍥 | `PolygonCollection` | `ax.collections` |
| `ax.text` - 鏂囨湰 | `Text` | `ax.texts` |
闄や簡鎵鏈夎繖浜涜壓鏈锛`Axes`鍖呭惈涓や釜閲嶈鐨勮壓鏈瀹瑰櫒锛`XAxis``YAxis`锛屽畠浠鐞嗗埢搴﹀拰鏍囩鐨勭粯鍒躲 瀹冧滑琚瓨鍌ㄤ负瀹炰緥鍙橀噺`xaxis``yaxis``XAxis``YAxis`瀹瑰櫒灏嗗湪涓嬮潰璇︾粏浠嬬粛锛屼絾璇锋敞鎰忥紝`Axes`鍖呭惈璁稿杈呭姪鏂规硶锛屽畠浠細灏嗚皟鐢ㄨ浆鍙戠粰`Axis`瀹炰緥锛屽洜姝ゆ偍閫氬父涓嶉渶瑕佺洿鎺ヤ娇鐢ㄥ畠浠紝闄ら潪鎮ㄦ効鎰忋 渚嬪锛屾偍鍙互浣跨敤`Axes`杈呭姪绋嬪簭鏂规硶璁剧疆`XAxis`鍒诲害鏍囩鐨勫瓧浣撳ぇ灏忥細
```py
for label in ax.get_xticklabels():
label.set_color('orange')
```
涓嬮潰鏄酱鍩熸墍鍖呭惈鐨勮壓鏈鐨勬荤粨
| 杞村煙灞炴 | 鎻忚堪 |
| --- | --- |
| `artists` | `Artist`瀹炰緥鐨勫垪琛 |
| `patch` | 鐢ㄤ簬杞村煙鑳屾櫙鐨`Rectangle`瀹炰緥 |
| collections | `Collection`瀹炰緥鐨勫垪琛 |
| images | `AxesImage`鐨勫垪琛 |
| legends | `Legend`瀹炰緥鐨勫垪琛 |
| lines | `Line2D`瀹炰緥鐨勫垪琛 |
| patches | `Patch`瀹炰緥鐨勫垪琛 |
| texts | `Text`瀹炰緥鐨勫垪琛 |
| xaxis | `matplotlib.axis.XAxis`瀹炰緥 |
| yaxis | `matplotlib.axis.YAxis`瀹炰緥 |
## 杞村鍣
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽