v2.0

涓婄骇 a0fa2377
# 3.1 pyplot 鏁欑▼
# pyplot 鏁欑▼
> 鍘熸枃锛歔Pyplot tutorial](http://matplotlib.org/users/pyplot_tutorial.html)
......
# 3.2 浣跨敤鏍峰紡琛ㄨ嚜瀹氫箟缁樺浘
# 鍥惧儚鏁欑▼
> 鍘熸枃锛歔Customizing plots with style sheets](http://matplotlib.org/users/style_sheets.html)
> 鍘熸枃锛歔Image tutorial](http://matplotlib.org/users/image_tutorial.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
`style`鍖呬负鏄撲簬鍒囨崲鐨勭粯鍥俱庢牱寮忋忓鍔犱簡鏀寔锛屽畠浠笌`matplotlibrc`鏂囦欢鍙傛暟鐩稿悓銆
## 鍚姩鍛戒护
鏈変竴浜涢瀹氫箟鏍峰紡鐢`matplotlib`鎻愪緵銆 渚嬪锛屾湁涓涓悕涓恒巊gplot銆忕殑棰勫畾涔夋牱寮忥紝瀹冩ā鎷`ggplot`锛圧 鐨勪竴绉嶆祦琛岀殑缁樺浘杞欢鍖咃級鐨勭編瀛︺ 涓轰簡浣跨敤姝ゆ牱寮忥紝鍙渶娣诲姞锛
棣栧厛锛岃鎴戜滑鍚姩 IPython銆 瀹冩槸 Python 鏍囧噯鎻愮ず绗︾殑鏈濂界殑鏀硅繘锛屽畠涓 Matplotlib 閰嶅悎寰楃浉褰撲笉閿欍 鍦 shell 鎴 IPython Notebook 涓婇兘鍙互鍚姩 IPython銆
闅忕潃 IPython 鍚姩锛屾垜浠幇鍦ㄩ渶瑕佽繛鎺ュ埌 GUI 浜嬩欢寰幆銆 瀹冨憡璇 IPython 鍦ㄥ摢閲岋紙浠ュ強濡備綍鏄剧ず锛夌粯鍥俱 瑕佽繛鎺ュ埌 GUI 寰幆锛岃鍦 IPython 鎻愮ず绗﹀鎵ц`%matplotlib`榄旀硶銆 鍦 [IPython 鐨 GUI 浜嬩欢寰幆鏂囨。](http://ipython.org/ipython-doc/2/interactive/reference.html#gui-event-loop-support)涓湁鏇村鐨勭粏鑺傘
濡傛灉浣跨敤 IPython Notebook锛屽彲浠ヤ娇鐢ㄧ浉鍚岀殑鍛戒护锛屼絾浜轰滑閫氬父浠ョ壒瀹氬弬鏁颁娇鐢`%matplotlib`
```
In [1]: %matplotlib inline
```
杩欏皢鎵撳紑鍐呰仈缁樺浘锛岀粯鍥惧浘褰㈠皢鏄剧ず鍦ㄧ瑪璁版湰涓 杩欏浜や簰鎬ф湁寰堥噸瑕佺殑褰卞搷銆 瀵逛簬鍐呰仈缁樺浘锛屽湪鍗曞厓鏍间笅鏂圭殑鍗曞厓鏍间腑杈撳嚭缁樺浘鐨勫懡浠や笉浼氬奖鍝嶇粯鍥俱 渚嬪锛屼粠鍒涘缓缁樺浘鐨勫崟鍏冩牸涓嬮潰鐨勫崟鍏冩牸鏇存敼棰滆壊琛ㄦ槸涓嶅彲鑳界殑銆 浣嗘槸锛屽浜庡叾浠栧悗绔紝渚嬪 qt4锛屽畠浠細鎵撳紑涓涓崟鐙殑绐楀彛锛岄偅浜涘垱寤虹粯鍥剧殑鍗曞厓鏍间笅鏂圭殑鍗曞厓鏍煎皢鏀瑰彉缁樺浘 - 瀹冩槸涓涓唴瀛樹腑鐨勬椿瀵硅薄銆
鏈暀绋嬪皢浣跨敤`matplotlib`鐨勫懡浠ゅ紡缁樺浘鎺ュ彛`pyplot`銆 璇ユ帴鍙g淮鎶ゅ叏灞鐘舵侊紝骞朵笖鍙敤浜庣畝鍗曞揩閫熷湴灏濊瘯鍚勭缁樺浘璁剧疆銆 鍙︿竴绉嶆槸闈㈠悜瀵硅薄鐨勬帴鍙o紝杩欎篃闈炲父寮哄ぇ锛屼竴鑸洿閫傚悎澶у瀷搴旂敤绋嬪簭鐨勫紑鍙戙 濡傛灉浣犳兂浜嗚В闈㈠悜瀵硅薄鎺ュ彛锛[浣跨敤涓婄殑甯歌闂](http://matplotlib.org/faq/usage_faq.html)鏄竴涓敤浜庤捣姝ョ殑涓嶉敊鐨勯〉闈€ 鐜板湪锛岃鎴戜滑缁х画浣跨敤鍛戒护寮忔柟寮忥細
```py
In [2]: import matplotlib.pyplot as plt
In [3]: import matplotlib.image as mpimg
In [4]: import numpy as np
```
## 灏嗗浘鍍忔暟鎹鍏ュ埌 NumPy 鏁扮粍
鍔犺浇鍥惧儚鏁版嵁鐢 Pillow 搴撴彁渚涙敮鎸併 鏈潵锛`matplotlib`鍙敮鎸 PNG 鍥惧儚銆 濡傛灉鏈満璇诲彇澶辫触锛屼笅闈㈡樉绀虹殑鍛戒护浼氬洖閫鍒 Pillow銆
姝ょず渚嬩腑浣跨敤鐨勫浘鍍忔槸 PNG 鏂囦欢锛屼絾鏄璁颁綇浣犺嚜宸辩殑鏁版嵁鐨 Pillow 瑕佹眰銆
涓嬮潰鏄垜浠鎽嗗紕鐨勫浘鐗囷細
![](http://matplotlib.org/_images/stinkbug.png)
瀹冩槸涓涓 24 浣 RGB PNG 鍥惧儚锛堟瘡涓 R锛孏锛孊 涓 8 浣嶏級銆 鏍规嵁浣犺幏鍙栨暟鎹殑浣嶇疆锛屼綘鏈鏈夊彲鑳介亣鍒扮殑鍏朵粬绫诲瀷鐨勫浘鍍忔槸 RGBA 鍥惧儚锛屾嫢鏈夐忔槑搴︽垨鍗曢氶亾鐏板害锛堜寒搴︼級鐨勫浘鍍忋 浣犲彲浠ュ彸閿崟鍑诲畠锛岄夋嫨`Save image as`锛堝彟瀛樹负锛変负鏈暀绋嬬殑鍓╀綑閮ㄥ垎涓嬭浇鍒颁綘鐨勮绠楁満銆
鐜板湪鎴戜滑寮濮...
```py
In [5]: img=mpimg.imread('stinkbug.png')
Out[5]:
array([[[ 0.40784314, 0.40784314, 0.40784314],
[ 0.40784314, 0.40784314, 0.40784314],
[ 0.40784314, 0.40784314, 0.40784314],
...,
[ 0.42745098, 0.42745098, 0.42745098],
[ 0.42745098, 0.42745098, 0.42745098],
[ 0.42745098, 0.42745098, 0.42745098]],
...,
[[ 0.44313726, 0.44313726, 0.44313726],
[ 0.4509804 , 0.4509804 , 0.4509804 ],
[ 0.4509804 , 0.4509804 , 0.4509804 ],
...,
[ 0.44705883, 0.44705883, 0.44705883],
[ 0.44705883, 0.44705883, 0.44705883],
[ 0.44313726, 0.44313726, 0.44313726]]], dtype=float32)
```
娉ㄦ剰杩欓噷鐨`dtype` - `float32`銆 Matplotlib 宸插皢姣忎釜閫氶亾鐨8浣嶆暟鎹噸鏂板畾鏍囦负 0.0 鍜 1.0 涔嬮棿鐨勬诞鐐规暟銆 浣滀负鏃佹敞锛孭illow 鍙互浣跨敤鐨勫敮涓鏁版嵁绫诲瀷鏄`uint8`銆 Matplotlib 缁樺浘鍙互澶勭悊`float32``uint8`锛屼絾鏄浜庨櫎 PNG 涔嬪鐨勪换浣曟牸寮忕殑鍥惧儚锛岃鍙/鍐欏叆浠呴檺浜`uint8`鏁版嵁銆 涓轰粈涔堟槸 8 浣嶅憿锛 澶у鏁版樉绀哄櫒鍙兘娓叉煋姣忛氶亾 8 浣嶇殑棰滆壊娓愬彉銆 涓轰粈涔堜粬浠彧鑳芥覆鏌撴瘡閫氶亾 8 浣嶅憿锛 鍥犱负杩欎細浣挎墍鏈変汉鐨勭溂鐫涘彲浠ョ湅鍒般 鏇村淇℃伅璇疯锛堜粠鎽勫奖鐨勮搴︼級锛[Luminous Landscape 浣嶆繁搴︽暀绋](http://www.luminous-landscape.com/tutorials/bit-depth.shtml)
姣忎釜鍐呴儴鍒楄〃琛ㄧず涓涓儚绱犮 杩欓噷锛屽浜 RGB 鍥惧儚锛屾湁 3 涓笺 鐢变簬瀹冩槸涓涓粦鐧藉浘鍍忥紝R锛孏 鍜 B 閮芥槸绫讳技鐨勩 RGBA锛堝叾涓 A 鏄樋灏旀硶鎴栭忔槑搴︼級瀵逛簬姣忎釜鍐呴儴鍒楄〃鍏锋湁 4 涓硷紝鑰屼笖绠鍗曚寒搴﹀浘鍍忎粎鍏锋湁涓涓硷紙鍥犳浠呮槸浜岀淮鏁扮粍锛岃屼笉鏄笁缁存暟缁勶級銆 瀵逛簬 RGB 鍜 RGBA 鍥惧儚锛`matplotlib`鏀寔`float32``uint8`鏁版嵁绫诲瀷銆 瀵逛簬鐏板害锛`matplotlib`鍙敮鎸`float32`銆 濡傛灉浣犵殑鏁扮粍鏁版嵁涓嶇鍚堣繖浜涙弿杩颁箣涓锛屽垯闇瑕侀噸鏂扮缉鏀惧畠銆
## 灏 NumPy 鏁扮粍缁樺埗涓哄浘鍍
鎵浠ワ紝浣犲皢鏁版嵁淇濆瓨鍦ㄤ竴涓`numpy`鏁扮粍锛堥氳繃瀵煎叆瀹冿紝鎴栫敓鎴愬畠锛夈 璁╂垜浠覆鏌撳畠鍚с 鍦 Matplotlib 涓紝杩欐槸浣跨敤`imshow()`鍑芥暟鎵ц鐨勩 杩欓噷鎴戜滑灏嗘姄鍙`plot`瀵硅薄銆 杩欎釜瀵硅薄鎻愪緵浜嗕竴涓畝鍗曠殑鏂规硶鏉ヤ粠鎻愮ず绗﹀鐞嗙粯鍥俱
```py
In [6]: imgplot = plt.imshow(img)
```
![](http://matplotlib.org/_images/image_tutorial-1.png)
浣犱篃鍙互缁樺埗浠讳綍 NumPy 鏁扮粍銆
### 瀵瑰浘鍍忕粯鍥惧簲鐢ㄤ吉褰╄壊鏂规
浼僵鑹插彲浠ユ槸涓涓湁鐢ㄧ殑宸ュ叿锛岀敤浜庡寮哄姣斿害鍜屾洿鏄撲簬鍙鍖栦綘鐨勬暟鎹 杩欏湪浣跨敤鎶曞奖浠浣犵殑鏁版嵁杩涜婕旂ず鏃跺挨鍏舵湁鐢 - 瀹冧滑鐨勫姣斿害閫氬父寰堝樊銆
浼僵鑹蹭粎涓庡崟閫氶亾锛岀伆搴︼紝浜害鍥惧儚鐩稿叧銆 鎴戜滑鐩墠鏈変竴涓猂GB鍥惧儚銆 鐢变簬R锛孏 鍜 B 閮芥槸鐩镐技鐨勶紙瑙佷笂闈㈡垨浣犵殑鏁版嵁锛夛紝鎴戜滑鍙互鍙夋嫨涓涓氶亾鐨勬暟鎹細
```py
>>> import matplotlib.pyplot as plt
>>> plt.style.use('ggplot')
In [7]: lum_img = img[:,:,0]
```
涓轰簡鍒楀嚭鎵鏈夊彲鐢ㄦ牱寮忥紝浣跨敤锛
杩欐槸鏁扮粍鍒囩墖锛屾洿澶氫俊鎭瑙[NumPy 鏁欑▼](http://www.scipy.org/Tentative_NumPy_Tutorial)
```py
>>> print(plt.style.available)
In [8]: plt.imshow(lum_img)
```
## 瀹氫箟浣犺嚜宸辩殑鏍峰紡
![](http://matplotlib.org/_images/image_tutorial-2.png)
浣犲彲浠ュ垱寤鸿嚜瀹氫箟鏍峰紡锛屽苟閫氳繃浠ユ牱寮忚〃鐨勮矾寰勬垨 URL 璋冪敤`style.use`鏉ヤ娇鐢ㄥ畠浠 鎴栬咃紝濡傛灉灏`<style-name> mplstyle`鏂囦欢娣诲姞鍒`mpl_configdir /stylelib`涓紝浣犲彲浠ラ氳繃璋冪敤`style.use(<style-name>)`閲嶅浣跨敤鑷畾涔夋牱寮忚〃銆 榛樿鎯呭喌涓`mpl_configdir`搴旇鏄`~/.config/matplotlib`锛屼絾浣犲彲浠ヤ娇鐢`matplotlib.get_configdir()`妫鏌ヤ綘鐨勪綅缃紝浣犲彲鑳介渶瑕佸垱寤鸿繖涓洰褰曘 璇锋敞鎰忥紝濡傛灉鏍峰紡鍏锋湁鐩稿悓鐨勫悕绉帮紝`mpl_configdir/stylelib`涓殑鑷畾涔夋牱寮忚〃灏嗚鐩栫敱`matplotlib`瀹氫箟鐨勬牱寮忚〃
鐜板湪锛屼寒搴︼紙2D锛屾棤棰滆壊锛夊浘鍍忓簲鐢ㄤ簡榛樿棰滆壊琛紙涔熺О涓烘煡鎵捐〃锛孡UT锛夈 榛樿鍊肩О涓`jet`銆 鏈夊緢澶氬叾浠栨柟妗堝彲浠ラ夋嫨
渚嬪锛屼綘鍙兘鎯宠浣跨敤浠ヤ笅鍛戒护鍒涘缓`mpl_configdir/stylelib/presentation.mplstyle`
```py
In [9]: plt.imshow(lum_img, cmap="hot")
```
![](http://matplotlib.org/_images/image_tutorial-3.png)
璇锋敞鎰忥紝浣犺繕鍙互浣跨敤`set_cmap()`鏂规硶鏇存敼鐜版湁缁樺浘瀵硅薄涓婄殑棰滆壊锛
```py
In [10]: imgplot = plt.imshow(lum_img)
In [11]: imgplot.set_cmap('spectral')
```
![](http://matplotlib.org/_images/image_tutorial-4.png)
> 娉
> 浣嗘槸锛岃璁颁綇锛屽湪甯︽湁鍐呰仈鍚庣鐨 IPython notebook 涓紝浣犱笉鑳藉宸茬粡娓叉煋鐨勭粯鍥捐繘琛屾洿鏀广 濡傛灉浣犲湪涓涓崟鍏冩牸涓垱寤轰簡`imgplot`锛屼綘涓嶈兘鍦ㄤ互鍚庣殑鍗曞厓鏍间腑璋冪敤`set_cmap()`锛屽苟涓旀敼鍙樺墠闈㈢殑缁樺浘銆 璇风‘淇濅綘鍦ㄧ浉鍚屽崟鍏冩牸涓竴璧疯緭鍏ヨ繖浜涘懡浠ゃ俙plt`鍛戒护涓嶄細鏇存敼鍏堝墠鍗曞厓鏍肩殑缁樺浘銆
鏈夎澶氬彲閫夌殑鍏跺畠棰滆壊琛紝璇疯[棰滆壊琛ㄧ殑鍒楄〃鍜屽浘鍍](http://matplotlib.org/examples/color/colormaps_reference.html)
### 棰滆壊鍒诲害鍙傝
浜嗚В棰滆壊浠h〃浠涔堝煎鎴戜滑寰堟湁甯姪銆 鎴戜滑鍙互閫氳繃娣诲姞棰滆壊鏉℃潵鍋氬埌杩欎竴鐐广
```py
In [12]: imgplot = plt.imshow(lum_img)
In [13]: plt.colorbar()
```
axes.titlesize : 24
axes.labelsize : 20
lines.linewidth : 3
lines.markersize : 10
xtick.labelsize : 16
ytick.labelsize : 16
![](http://matplotlib.org/_images/image_tutorial-5.png)
杩欎細涓轰綘鐜版湁鐨勫浘褰㈡坊鍔犱竴涓鑹叉潯銆 濡傛灉浣犳洿鏀瑰苟鍒囨崲鍒颁笉鍚岀殑棰滆壊鏄犲皠锛屽垯涓嶄細鑷姩鏇存敼 - 浣犲繀椤婚噸鏂板垱寤虹粯鍥撅紝骞跺啀娆℃坊鍔犻鑹叉潯銆
### 妫鏌ョ壒瀹氭暟鎹寖鍥
鏈夋椂锛屼綘鎯宠澧炲己鍥惧儚鐨勫姣斿害锛屾垨鑰呮墿澶х壒瀹氬尯鍩熺殑瀵规瘮搴︼紝鍚屾椂鐗虹壊鍙樺寲涓嶅ぇ锛屾垨鑰呮棤鎵璋撶殑棰滆壊缁嗚妭銆 鎵惧埌鏈夎叮鍖哄煙鐨勬渶濂藉伐鍏锋槸鐩存柟鍥俱 瑕佸垱寤烘垜浠殑鍥惧儚鏁版嵁鐨勭洿鏂瑰浘锛屾垜浠娇鐢`hist()`鍑芥暟銆
```py
In [14]: plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
```
鐒跺悗锛屽綋浣犳兂瑕佸皢涓涓负绾稿紶璁捐鐨勫湴鍥捐縼绉诲埌婕旂ず鏂囨。涓椂锛屼綘鍙互娣诲姞锛
![](http://matplotlib.org/_images/image_tutorial-6.png)
閫氬父锛屽浘鍍忕殑銆庢湁瓒c忛儴鍒嗗湪宄板奸檮杩戯紝浣犲彲浠ラ氳繃鍓垏宄板间笂鏂瑰拰/鎴栦笅鏂圭殑鍖哄煙鑾峰緱棰濆鐨勫姣斿害銆 鍦ㄦ垜浠殑鐩存柟鍥句腑锛岀湅璧锋潵鏈澶у煎娌℃湁澶鏈夌敤鐨勪俊鎭紙鍥惧儚涓湁寰堝涓嶆槸鐧借壊鐨勪笢瑗匡級銆 璁╂垜浠皟鏁翠笂闄愶紝浠ヤ究鎴戜滑鏈夋晥鍦般庢斁澶с忕洿鏂瑰浘鐨勪竴閮ㄥ垎銆 鎴戜滑閫氳繃灏`clim`鍙傛暟浼犻掔粰`imshow`鏉ュ疄鐜般 浣犱篃鍙互閫氳繃瀵瑰浘鍍忕粯鍥惧璞¤皟鐢`set_clim()`鏂规硶鏉ュ仛鍒拌繖涓鐐癸紝浣嗚纭繚浣犲湪浣跨敤 IPython Notebook 鐨勬椂鍊欙紝鍜`plot`鍛戒护鍦ㄧ浉鍚岀殑鍗曞厓鏍间腑鎵ц - 瀹冧笉浼氭敼鍙樹箣鍓嶅崟鍏冩牸鐨勫浘銆
```py
>>> import matplotlib.pyplot as plt
>>> plt.style.use('presentation')
In [15]: imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
```
## 缁勫悎鏍峰紡
![](http://matplotlib.org/_images/image_tutorial-7.png)
### 鏁扮粍鎻掑兼柟妗
鎻掑兼牴鎹笉鍚岀殑鏁板鏂规璁$畻鍍忕礌銆庡簲鏈夈忕殑棰滆壊鎴栧笺 鍙戠敓杩欑鎯呭喌鐨勪竴涓父瑙佺殑鍦烘櫙鏄皟鏁村浘鍍忕殑澶у皬銆 鍍忕礌鐨勬暟閲忎細鍙戠敓鍙樺寲锛屼絾浣犳兂瑕佺浉鍚岀殑淇℃伅銆 鐢变簬鍍忕礌鏄鏁g殑锛屽洜姝ゅ瓨鍦ㄧ己澶辩殑绌洪棿銆 鎻掑煎氨鏄~琛ヨ繖涓┖闂寸殑鏂瑰紡銆 杩欏氨鏄綋浣犳斁澶у浘鍍忔椂锛屼綘鐨勫浘鍍忔湁鏃朵細鍑烘潵鐪嬭捣鏉ュ儚绱犲寲鐨勫師鍥犮 褰撳師濮嬪浘鍍忓拰鎵╁睍鍥惧儚涔嬮棿鐨勫樊寮傝緝澶ф椂锛屾晥鏋滄洿鍔犳槑鏄俱 璁╂垜浠姞杞芥垜浠殑鍥惧儚骞剁缉灏忓畠銆 鎴戜滑瀹為檯涓婃鍦ㄤ涪寮冨儚绱狅紝鍙繚鐣欏皯鏁板嚑涓儚绱犮 鐜板湪锛屽綋鎴戜滑缁樺埗瀹冩椂锛屾暟鎹鏀惧ぇ涓轰綘灞忓箷鐨勫ぇ灏忋 鐢变簬鏃х殑鍍忕礌涓嶅啀瀛樺湪锛岃绠楁満蹇呴』缁樺埗鍍忕礌鏉ュ~鍏呴偅涓┖闂淬
鏍峰紡琛ㄤ负缁勫悎鍦ㄤ竴璧疯岃璁° 鍥犳锛屼綘鍙互鎷ユ湁涓涓嚜瀹氫箟棰滆壊鐨勬牱寮忚〃鍜屼竴涓崟鐙殑鏍峰紡琛紝鐢ㄤ簬鏇存敼婕旂ず鏂囨。鐨勫厓绱犲ぇ灏忋 杩欎簺鏍峰紡鍙互閫氳繃浼犻掓牱寮忓垪琛ㄨ交鏉剧粍鍚堬細
鎴戜滑灏嗕娇鐢ㄧ敤鏉ュ姞杞藉浘鍍忕殑 Pillow 搴撴潵璋冩暣鍥惧儚澶у皬銆
```py
>>> import matplotlib.pyplot as plt
>>> plt.style.use(['dark_background', 'presentation'])
In [16]: from PIL import Image
In [17]: img = Image.open('../_static/stinkbug.png')
In [18]: img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
In [19]: imgplot = plt.imshow(img)
```
璇锋敞鎰忥紝鍙充晶鐨勬牱寮忓皢瑕嗙洊宸茬粡鐢卞乏渚ф牱寮忓畾涔夌殑鍊笺
![](http://matplotlib.org/_images/image_tutorial-8.png)
## 涓存椂鏍峰紡
杩欓噷鎴戜滑浣跨敤榛樿鎻掑硷紝鍙岀嚎鎬э紝鍥犱负鎴戜滑娌℃湁鍚`imshow()`鎻愪緵浠讳綍鎻掑煎弬鏁般
濡傛灉鍙兂瀵圭壒瀹氱殑浠g爜鍧椾娇鐢ㄦ牱寮忥紝浣嗕笉鎯虫洿鏀瑰叏灞鏍峰紡锛岄偅涔堟牱寮忓寘鎻愪緵浜嗕竴涓笂涓嬫枃绠$悊鍣紝鐢ㄤ簬灏嗘洿鏀归檺鍒朵簬鐗瑰畾鑼冨洿銆 瑕侀殧绂讳綘鐨勬牱寮忔洿鏀癸紝浣犲彲浠ョ紪鍐欎互涓嬪唴瀹癸細
璁╂垜浠瘯璇曚竴浜涘叾瀹冪殑涓滆タ锛
鏈閭昏繎
```py
In [20]: imgplot = plt.imshow(img, interpolation="nearest")
```
![](http://matplotlib.org/_images/image_tutorial-9.png)
鍙岀珛鏂
```py
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> with plt.style.context(('dark_background')):
>>> plt.plot(np.sin(np.linspace(0, 2 * np.pi)), 'r-o')
>>>
>>> # Some plotting code with the default style
>>>
>>> plt.show()
In [21]: imgplot = plt.imshow(img, interpolation="bicubic")
```
![](http://matplotlib.org/_images/image_tutorial-10.png)
鍙岀珛鏂规彃鍊奸氬父鐢ㄤ簬鏀惧ぇ鐓х墖 - 浜轰滑鍊惧悜浜庢ā绯婅屼笉鏄繃搴﹀儚绱犲寲銆
# 3.5 鍥惧儚鏁欑▼
> 鍘熸枃锛歔Image tutorial](http://matplotlib.org/users/image_tutorial.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
## 鍚姩鍛戒护
棣栧厛锛岃鎴戜滑鍚姩 IPython銆 瀹冩槸 Python 鏍囧噯鎻愮ず绗︾殑鏈濂界殑鏀硅繘锛屽畠涓 Matplotlib 閰嶅悎寰楃浉褰撲笉閿欍 鍦 shell 鎴 IPython Notebook 涓婇兘鍙互鍚姩 IPython銆
闅忕潃 IPython 鍚姩锛屾垜浠幇鍦ㄩ渶瑕佽繛鎺ュ埌 GUI 浜嬩欢寰幆銆 瀹冨憡璇 IPython 鍦ㄥ摢閲岋紙浠ュ強濡備綍鏄剧ず锛夌粯鍥俱 瑕佽繛鎺ュ埌 GUI 寰幆锛岃鍦 IPython 鎻愮ず绗﹀鎵ц`%matplotlib`榄旀硶銆 鍦 [IPython 鐨 GUI 浜嬩欢寰幆鏂囨。](http://ipython.org/ipython-doc/2/interactive/reference.html#gui-event-loop-support)涓湁鏇村鐨勭粏鑺傘
濡傛灉浣跨敤 IPython Notebook锛屽彲浠ヤ娇鐢ㄧ浉鍚岀殑鍛戒护锛屼絾浜轰滑閫氬父浠ョ壒瀹氬弬鏁颁娇鐢`%matplotlib`
```
In [1]: %matplotlib inline
```
杩欏皢鎵撳紑鍐呰仈缁樺浘锛岀粯鍥惧浘褰㈠皢鏄剧ず鍦ㄧ瑪璁版湰涓 杩欏浜や簰鎬ф湁寰堥噸瑕佺殑褰卞搷銆 瀵逛簬鍐呰仈缁樺浘锛屽湪鍗曞厓鏍间笅鏂圭殑鍗曞厓鏍间腑杈撳嚭缁樺浘鐨勫懡浠や笉浼氬奖鍝嶇粯鍥俱 渚嬪锛屼粠鍒涘缓缁樺浘鐨勫崟鍏冩牸涓嬮潰鐨勫崟鍏冩牸鏇存敼棰滆壊琛ㄦ槸涓嶅彲鑳界殑銆 浣嗘槸锛屽浜庡叾浠栧悗绔紝渚嬪 qt4锛屽畠浠細鎵撳紑涓涓崟鐙殑绐楀彛锛岄偅浜涘垱寤虹粯鍥剧殑鍗曞厓鏍间笅鏂圭殑鍗曞厓鏍煎皢鏀瑰彉缁樺浘 - 瀹冩槸涓涓唴瀛樹腑鐨勬椿瀵硅薄銆
鏈暀绋嬪皢浣跨敤`matplotlib`鐨勫懡浠ゅ紡缁樺浘鎺ュ彛`pyplot`銆 璇ユ帴鍙g淮鎶ゅ叏灞鐘舵侊紝骞朵笖鍙敤浜庣畝鍗曞揩閫熷湴灏濊瘯鍚勭缁樺浘璁剧疆銆 鍙︿竴绉嶆槸闈㈠悜瀵硅薄鐨勬帴鍙o紝杩欎篃闈炲父寮哄ぇ锛屼竴鑸洿閫傚悎澶у瀷搴旂敤绋嬪簭鐨勫紑鍙戙 濡傛灉浣犳兂浜嗚В闈㈠悜瀵硅薄鎺ュ彛锛[浣跨敤涓婄殑甯歌闂](http://matplotlib.org/faq/usage_faq.html)鏄竴涓敤浜庤捣姝ョ殑涓嶉敊鐨勯〉闈€ 鐜板湪锛岃鎴戜滑缁х画浣跨敤鍛戒护寮忔柟寮忥細
```py
In [2]: import matplotlib.pyplot as plt
In [3]: import matplotlib.image as mpimg
In [4]: import numpy as np
```
## 灏嗗浘鍍忔暟鎹鍏ュ埌 NumPy 鏁扮粍
鍔犺浇鍥惧儚鏁版嵁鐢 Pillow 搴撴彁渚涙敮鎸併 鏈潵锛`matplotlib`鍙敮鎸 PNG 鍥惧儚銆 濡傛灉鏈満璇诲彇澶辫触锛屼笅闈㈡樉绀虹殑鍛戒护浼氬洖閫鍒 Pillow銆
姝ょず渚嬩腑浣跨敤鐨勫浘鍍忔槸 PNG 鏂囦欢锛屼絾鏄璁颁綇浣犺嚜宸辩殑鏁版嵁鐨 Pillow 瑕佹眰銆
涓嬮潰鏄垜浠鎽嗗紕鐨勫浘鐗囷細
![](http://matplotlib.org/_images/stinkbug.png)
瀹冩槸涓涓 24 浣 RGB PNG 鍥惧儚锛堟瘡涓 R锛孏锛孊 涓 8 浣嶏級銆 鏍规嵁浣犺幏鍙栨暟鎹殑浣嶇疆锛屼綘鏈鏈夊彲鑳介亣鍒扮殑鍏朵粬绫诲瀷鐨勫浘鍍忔槸 RGBA 鍥惧儚锛屾嫢鏈夐忔槑搴︽垨鍗曢氶亾鐏板害锛堜寒搴︼級鐨勫浘鍍忋 浣犲彲浠ュ彸閿崟鍑诲畠锛岄夋嫨`Save image as`锛堝彟瀛樹负锛変负鏈暀绋嬬殑鍓╀綑閮ㄥ垎涓嬭浇鍒颁綘鐨勮绠楁満銆
鐜板湪鎴戜滑寮濮...
```py
In [5]: img=mpimg.imread('stinkbug.png')
Out[5]:
array([[[ 0.40784314, 0.40784314, 0.40784314],
[ 0.40784314, 0.40784314, 0.40784314],
[ 0.40784314, 0.40784314, 0.40784314],
...,
[ 0.42745098, 0.42745098, 0.42745098],
[ 0.42745098, 0.42745098, 0.42745098],
[ 0.42745098, 0.42745098, 0.42745098]],
...,
[[ 0.44313726, 0.44313726, 0.44313726],
[ 0.4509804 , 0.4509804 , 0.4509804 ],
[ 0.4509804 , 0.4509804 , 0.4509804 ],
...,
[ 0.44705883, 0.44705883, 0.44705883],
[ 0.44705883, 0.44705883, 0.44705883],
[ 0.44313726, 0.44313726, 0.44313726]]], dtype=float32)
```
娉ㄦ剰杩欓噷鐨`dtype` - `float32`銆 Matplotlib 宸插皢姣忎釜閫氶亾鐨8浣嶆暟鎹噸鏂板畾鏍囦负 0.0 鍜 1.0 涔嬮棿鐨勬诞鐐规暟銆 浣滀负鏃佹敞锛孭illow 鍙互浣跨敤鐨勫敮涓鏁版嵁绫诲瀷鏄`uint8`銆 Matplotlib 缁樺浘鍙互澶勭悊`float32``uint8`锛屼絾鏄浜庨櫎 PNG 涔嬪鐨勪换浣曟牸寮忕殑鍥惧儚锛岃鍙/鍐欏叆浠呴檺浜`uint8`鏁版嵁銆 涓轰粈涔堟槸 8 浣嶅憿锛 澶у鏁版樉绀哄櫒鍙兘娓叉煋姣忛氶亾 8 浣嶇殑棰滆壊娓愬彉銆 涓轰粈涔堜粬浠彧鑳芥覆鏌撴瘡閫氶亾 8 浣嶅憿锛 鍥犱负杩欎細浣挎墍鏈変汉鐨勭溂鐫涘彲浠ョ湅鍒般 鏇村淇℃伅璇疯锛堜粠鎽勫奖鐨勮搴︼級锛[Luminous Landscape 浣嶆繁搴︽暀绋](http://www.luminous-landscape.com/tutorials/bit-depth.shtml)
姣忎釜鍐呴儴鍒楄〃琛ㄧず涓涓儚绱犮 杩欓噷锛屽浜 RGB 鍥惧儚锛屾湁 3 涓笺 鐢变簬瀹冩槸涓涓粦鐧藉浘鍍忥紝R锛孏 鍜 B 閮芥槸绫讳技鐨勩 RGBA锛堝叾涓 A 鏄樋灏旀硶鎴栭忔槑搴︼級瀵逛簬姣忎釜鍐呴儴鍒楄〃鍏锋湁 4 涓硷紝鑰屼笖绠鍗曚寒搴﹀浘鍍忎粎鍏锋湁涓涓硷紙鍥犳浠呮槸浜岀淮鏁扮粍锛岃屼笉鏄笁缁存暟缁勶級銆 瀵逛簬 RGB 鍜 RGBA 鍥惧儚锛`matplotlib`鏀寔`float32``uint8`鏁版嵁绫诲瀷銆 瀵逛簬鐏板害锛`matplotlib`鍙敮鎸`float32`銆 濡傛灉浣犵殑鏁扮粍鏁版嵁涓嶇鍚堣繖浜涙弿杩颁箣涓锛屽垯闇瑕侀噸鏂扮缉鏀惧畠銆
## 灏 NumPy 鏁扮粍缁樺埗涓哄浘鍍
鎵浠ワ紝浣犲皢鏁版嵁淇濆瓨鍦ㄤ竴涓`numpy`鏁扮粍锛堥氳繃瀵煎叆瀹冿紝鎴栫敓鎴愬畠锛夈 璁╂垜浠覆鏌撳畠鍚с 鍦 Matplotlib 涓紝杩欐槸浣跨敤`imshow()`鍑芥暟鎵ц鐨勩 杩欓噷鎴戜滑灏嗘姄鍙`plot`瀵硅薄銆 杩欎釜瀵硅薄鎻愪緵浜嗕竴涓畝鍗曠殑鏂规硶鏉ヤ粠鎻愮ず绗﹀鐞嗙粯鍥俱
```py
In [6]: imgplot = plt.imshow(img)
```
![](http://matplotlib.org/_images/image_tutorial-1.png)
浣犱篃鍙互缁樺埗浠讳綍 NumPy 鏁扮粍銆
### 瀵瑰浘鍍忕粯鍥惧簲鐢ㄤ吉褰╄壊鏂规
浼僵鑹插彲浠ユ槸涓涓湁鐢ㄧ殑宸ュ叿锛岀敤浜庡寮哄姣斿害鍜屾洿鏄撲簬鍙鍖栦綘鐨勬暟鎹 杩欏湪浣跨敤鎶曞奖浠浣犵殑鏁版嵁杩涜婕旂ず鏃跺挨鍏舵湁鐢 - 瀹冧滑鐨勫姣斿害閫氬父寰堝樊銆
浼僵鑹蹭粎涓庡崟閫氶亾锛岀伆搴︼紝浜害鍥惧儚鐩稿叧銆 鎴戜滑鐩墠鏈変竴涓猂GB鍥惧儚銆 鐢变簬R锛孏 鍜 B 閮芥槸鐩镐技鐨勶紙瑙佷笂闈㈡垨浣犵殑鏁版嵁锛夛紝鎴戜滑鍙互鍙夋嫨涓涓氶亾鐨勬暟鎹細
```py
In [7]: lum_img = img[:,:,0]
```
杩欐槸鏁扮粍鍒囩墖锛屾洿澶氫俊鎭瑙[NumPy 鏁欑▼](http://www.scipy.org/Tentative_NumPy_Tutorial)
```py
In [8]: plt.imshow(lum_img)
```
![](http://matplotlib.org/_images/image_tutorial-2.png)
鐜板湪锛屼寒搴︼紙2D锛屾棤棰滆壊锛夊浘鍍忓簲鐢ㄤ簡榛樿棰滆壊琛紙涔熺О涓烘煡鎵捐〃锛孡UT锛夈 榛樿鍊肩О涓`jet`銆 鏈夊緢澶氬叾浠栨柟妗堝彲浠ラ夋嫨銆
```py
In [9]: plt.imshow(lum_img, cmap="hot")
```
![](http://matplotlib.org/_images/image_tutorial-3.png)
璇锋敞鎰忥紝浣犺繕鍙互浣跨敤`set_cmap()`鏂规硶鏇存敼鐜版湁缁樺浘瀵硅薄涓婄殑棰滆壊锛
```py
In [10]: imgplot = plt.imshow(lum_img)
In [11]: imgplot.set_cmap('spectral')
```
![](http://matplotlib.org/_images/image_tutorial-4.png)
> 娉
> 浣嗘槸锛岃璁颁綇锛屽湪甯︽湁鍐呰仈鍚庣鐨 IPython notebook 涓紝浣犱笉鑳藉宸茬粡娓叉煋鐨勭粯鍥捐繘琛屾洿鏀广 濡傛灉浣犲湪涓涓崟鍏冩牸涓垱寤轰簡`imgplot`锛屼綘涓嶈兘鍦ㄤ互鍚庣殑鍗曞厓鏍间腑璋冪敤`set_cmap()`锛屽苟涓旀敼鍙樺墠闈㈢殑缁樺浘銆 璇风‘淇濅綘鍦ㄧ浉鍚屽崟鍏冩牸涓竴璧疯緭鍏ヨ繖浜涘懡浠ゃ俙plt`鍛戒护涓嶄細鏇存敼鍏堝墠鍗曞厓鏍肩殑缁樺浘銆
鏈夎澶氬彲閫夌殑鍏跺畠棰滆壊琛紝璇疯[棰滆壊琛ㄧ殑鍒楄〃鍜屽浘鍍](http://matplotlib.org/examples/color/colormaps_reference.html)
### 棰滆壊鍒诲害鍙傝
浜嗚В棰滆壊浠h〃浠涔堝煎鎴戜滑寰堟湁甯姪銆 鎴戜滑鍙互閫氳繃娣诲姞棰滆壊鏉℃潵鍋氬埌杩欎竴鐐广
```py
In [12]: imgplot = plt.imshow(lum_img)
In [13]: plt.colorbar()
```
![](http://matplotlib.org/_images/image_tutorial-5.png)
杩欎細涓轰綘鐜版湁鐨勫浘褰㈡坊鍔犱竴涓鑹叉潯銆 濡傛灉浣犳洿鏀瑰苟鍒囨崲鍒颁笉鍚岀殑棰滆壊鏄犲皠锛屽垯涓嶄細鑷姩鏇存敼 - 浣犲繀椤婚噸鏂板垱寤虹粯鍥撅紝骞跺啀娆℃坊鍔犻鑹叉潯銆
### 妫鏌ョ壒瀹氭暟鎹寖鍥
鏈夋椂锛屼綘鎯宠澧炲己鍥惧儚鐨勫姣斿害锛屾垨鑰呮墿澶х壒瀹氬尯鍩熺殑瀵规瘮搴︼紝鍚屾椂鐗虹壊鍙樺寲涓嶅ぇ锛屾垨鑰呮棤鎵璋撶殑棰滆壊缁嗚妭銆 鎵惧埌鏈夎叮鍖哄煙鐨勬渶濂藉伐鍏锋槸鐩存柟鍥俱 瑕佸垱寤烘垜浠殑鍥惧儚鏁版嵁鐨勭洿鏂瑰浘锛屾垜浠娇鐢`hist()`鍑芥暟銆
```py
In [14]: plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
```
![](http://matplotlib.org/_images/image_tutorial-6.png)
閫氬父锛屽浘鍍忕殑銆庢湁瓒c忛儴鍒嗗湪宄板奸檮杩戯紝浣犲彲浠ラ氳繃鍓垏宄板间笂鏂瑰拰/鎴栦笅鏂圭殑鍖哄煙鑾峰緱棰濆鐨勫姣斿害銆 鍦ㄦ垜浠殑鐩存柟鍥句腑锛岀湅璧锋潵鏈澶у煎娌℃湁澶鏈夌敤鐨勪俊鎭紙鍥惧儚涓湁寰堝涓嶆槸鐧借壊鐨勪笢瑗匡級銆 璁╂垜浠皟鏁翠笂闄愶紝浠ヤ究鎴戜滑鏈夋晥鍦般庢斁澶с忕洿鏂瑰浘鐨勪竴閮ㄥ垎銆 鎴戜滑閫氳繃灏`clim`鍙傛暟浼犻掔粰`imshow`鏉ュ疄鐜般 浣犱篃鍙互閫氳繃瀵瑰浘鍍忕粯鍥惧璞¤皟鐢`set_clim()`鏂规硶鏉ュ仛鍒拌繖涓鐐癸紝浣嗚纭繚浣犲湪浣跨敤 IPython Notebook 鐨勬椂鍊欙紝鍜`plot`鍛戒护鍦ㄧ浉鍚岀殑鍗曞厓鏍间腑鎵ц - 瀹冧笉浼氭敼鍙樹箣鍓嶅崟鍏冩牸鐨勫浘銆
```py
In [15]: imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
```
![](http://matplotlib.org/_images/image_tutorial-7.png)
### 鏁扮粍鎻掑兼柟妗
鎻掑兼牴鎹笉鍚岀殑鏁板鏂规璁$畻鍍忕礌銆庡簲鏈夈忕殑棰滆壊鎴栧笺 鍙戠敓杩欑鎯呭喌鐨勪竴涓父瑙佺殑鍦烘櫙鏄皟鏁村浘鍍忕殑澶у皬銆 鍍忕礌鐨勬暟閲忎細鍙戠敓鍙樺寲锛屼絾浣犳兂瑕佺浉鍚岀殑淇℃伅銆 鐢变簬鍍忕礌鏄鏁g殑锛屽洜姝ゅ瓨鍦ㄧ己澶辩殑绌洪棿銆 鎻掑煎氨鏄~琛ヨ繖涓┖闂寸殑鏂瑰紡銆 杩欏氨鏄綋浣犳斁澶у浘鍍忔椂锛屼綘鐨勫浘鍍忔湁鏃朵細鍑烘潵鐪嬭捣鏉ュ儚绱犲寲鐨勫師鍥犮 褰撳師濮嬪浘鍍忓拰鎵╁睍鍥惧儚涔嬮棿鐨勫樊寮傝緝澶ф椂锛屾晥鏋滄洿鍔犳槑鏄俱 璁╂垜浠姞杞芥垜浠殑鍥惧儚骞剁缉灏忓畠銆 鎴戜滑瀹為檯涓婃鍦ㄤ涪寮冨儚绱狅紝鍙繚鐣欏皯鏁板嚑涓儚绱犮 鐜板湪锛屽綋鎴戜滑缁樺埗瀹冩椂锛屾暟鎹鏀惧ぇ涓轰綘灞忓箷鐨勫ぇ灏忋 鐢变簬鏃х殑鍍忕礌涓嶅啀瀛樺湪锛岃绠楁満蹇呴』缁樺埗鍍忕礌鏉ュ~鍏呴偅涓┖闂淬
鎴戜滑灏嗕娇鐢ㄧ敤鏉ュ姞杞藉浘鍍忕殑 Pillow 搴撴潵璋冩暣鍥惧儚澶у皬銆
```py
In [16]: from PIL import Image
In [17]: img = Image.open('../_static/stinkbug.png')
In [18]: img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
In [19]: imgplot = plt.imshow(img)
```
![](http://matplotlib.org/_images/image_tutorial-8.png)
杩欓噷鎴戜滑浣跨敤榛樿鎻掑硷紝鍙岀嚎鎬э紝鍥犱负鎴戜滑娌℃湁鍚`imshow()`鎻愪緵浠讳綍鎻掑煎弬鏁般
璁╂垜浠瘯璇曚竴浜涘叾瀹冪殑涓滆タ锛
鏈閭昏繎
```py
In [20]: imgplot = plt.imshow(img, interpolation="nearest")
```
![](http://matplotlib.org/_images/image_tutorial-9.png)
鍙岀珛鏂
```py
In [21]: imgplot = plt.imshow(img, interpolation="bicubic")
```
![](http://matplotlib.org/_images/image_tutorial-10.png)
鍙岀珛鏂规彃鍊奸氬父鐢ㄤ簬鏀惧ぇ鐓х墖 - 浜轰滑鍊惧悜浜庢ā绯婅屼笉鏄繃搴﹀儚绱犲寲銆
# 3.6 鍥句緥鎸囧崡
# 鍥句緥鎸囧崡
> 鍘熸枃锛歔Legend guide](http://matplotlib.org/users/legend_guide.html)
......
# 绗笁绔 鍏ラ棬鎸囧崡
# 鏁欑▼
# 寮曡█
> 鍘熸枃锛歔Text introduction](http://matplotlib.org/users/text_intro.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
matplotlib 鍏锋湁浼樼鐨勬枃鏈敮鎸侊紝鍖呮嫭鏁板琛ㄨ揪寮忥紝鍏夋爡鍜屽悜閲忚緭鍑虹殑 truetype 鏀寔锛屼换鎰忔棆杞殑鎹㈣鍒嗛殧鏂囨湰鍜 unicode 鏀寔銆 鍥犱负鎴戜滑鐩存帴鍦ㄨ緭鍑烘枃妗d腑宓屽叆瀛椾綋锛屼緥濡 postscript 鎴 PDF锛屼綘鍦ㄥ睆骞曚笂鐪嬪埌鐨勪篃鏄綘鍦ㄦ墦鍗颁欢涓緱鍒扮殑銆 freetype2 鍙骇鐢熼潪甯告紓浜紝鎶楅敮榻跨殑瀛椾綋锛屽嵆浣垮湪灏忓厜鏍呭昂瀵镐笅鐪嬭捣鏉ヤ篃涓嶉敊銆 matplotlib 鎷ユ湁鑷繁鐨`matplotlib.font_manager`锛屾劅璋 Paul Barrett锛屼粬瀹炵幇浜嗕竴涓法骞冲彴锛岀鍚 W3C 鏍囧噯鐨勫瓧浣撴煡鎵剧畻娉曘
浣犲彲浠ュ畬鍏ㄦ帶鍒舵瘡涓枃鏈睘鎬э紙瀛椾綋澶у皬锛屽瓧浣撻噸閲忥紝鏂囨湰浣嶇疆鍜岄鑹茬瓑锛夛紝骞跺湪`rc`鏂囦欢涓缃悎鐞嗙殑榛樿鍊笺 骞朵笖瀵逛簬閭d簺瀵规暟瀛︽垨绉戝鍥惧儚鎰熷叴瓒g殑浜猴紝matplotlib 瀹炵幇浜嗗ぇ閲忕殑 TeX 鏁板绗﹀彿鍜屽懡浠わ紝鏉ユ敮鎸佷綘鍥句腑浠讳綍鍦版柟鏀剧疆鏁板琛ㄨ揪寮忋
\ No newline at end of file
# 鍩烘湰鐨勬枃鏈懡浠
> 鍘熸枃锛歔Basic text commands](http://matplotlib.org/users/text_intro.html#basic-text-commands)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
## text
`Axes`鐨勪换鎰忎綅缃坊鍔犳枃鏈
鍛戒护寮忥細[`matplotlib.pyplot.text`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text)锛岄潰鍚戝璞★細[`matplotlib.axes.Axes.text`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.text)
## xlabel
鍚 x 杞存坊鍔犺酱鏍囩銆
鍛戒护寮忥細[`matplotlib.pyplot.xlabel`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel)锛岄潰鍚戝璞★細[`matplotlib.axes.Axes.set_xlabel`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_xlabel)
## ylabel
鍚 y 杞存坊鍔犺酱鏍囩銆
鍛戒护寮忥細[`matplotlib.pyplot.ylabel`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.ylabel)锛岄潰鍚戝璞★細[`matplotlib.axes.Axes.set_ylabel`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_ylabel)
## title
`Axes`娣诲姞鏍囬銆
鍛戒护寮忥細[`matplotlib.pyplot.title`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.title)锛岄潰鍚戝璞★細[`matplotlib.axes.Axes.set_title`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_title)
## figtext
`Figure`鐨勪换鎰忎綅缃坊鍔犳枃鏈
鍛戒护寮忥細[`matplotlib.pyplot.figtext`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figtext)锛岄潰鍚戝璞★細[`matplotlib.figure.Figure.text`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.text)
## suptitle
`Figure`娣诲姞鏍囬銆
鍛戒护寮忥細[`matplotlib.pyplot.suptitle`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.suptitle)锛岄潰鍚戝璞★細[`matplotlib.figure.Figure.suptitle`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.suptitle)
## annotate
`Axes`娣诲姞鏍囨敞锛屽甫鏈夊彲閫夌殑绠ご銆
鍛戒护寮忥細[`matplotlib.pyplot.annotate`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.annotate)锛岄潰鍚戝璞★細[`matplotlib.axes.Axes.annotate`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.annotate)
鎵鏈夎繖浜涘嚱鏁板垱寤哄苟杩斿洖涓涓`matplotlib.text.Text()`瀹炰緥锛屽畠鍙互閰嶇疆鍚勭瀛椾綋鍜屽叾浠栧睘鎬с 涓嬮潰鐨勭ず渚嬪湪瀹炴垬涓睍绀烘墍鏈夎繖浜涘懡浠ゃ
```py
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
ax.text(3, 2, u'unicode: Institut f\374r Festk\366rperphysik')
ax.text(0.95, 0.01, 'colored text in axes coords',
verticalalignment='bottom', horizontalalignment='right',
transform=ax.transAxes,
color='green', fontsize=15)
ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.axis([0, 10, 0, 10])
plt.show()
```
![](http://matplotlib.org/_images/text_commands.png)
\ No newline at end of file
# 鏂囨湰灞炴у強甯冨眬
> 鍘熸枃锛歔Text properties and layout](http://matplotlib.org/users/text_props.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
`matplotlib.text.Text`瀹炰緥鏈夊悇绉嶅睘鎬э紝鍙互閫氳繃鍏抽敭瀛楀弬鏁伴厤缃枃鏈懡浠わ紙渚嬪锛`title()``xlabel()``text()`锛夈
| 灞炴 | 鍊肩被鍨 |
| --- | --- |
| alpha | 娴偣 |
| backgroundcolor | 浠讳綍 matplotlib 棰滆壊 |
| bbox | rectangle prop dict plus key 'pad' which is a pad in points |
| clip_box | `matplotlib.transform.Bbox` 瀹炰緥 |
| clip_on | `[True / False]` |
| clip_path | `Path` 瀹炰緥鍜 `Transform` 瀹炰緥锛`Patch` |
| color | 浠讳綍 matplotlib 棰滆壊 |
| family | `[ 'serif' / 'sans-serif' / 'cursive' / 'fantasy' / 'monospace' ]` |
| fontproperties | `matplotlib.font_manager.FontProperties` 瀹炰緥 |
| horizontalalignment or ha | `[ 'center' / 'right' / 'left' ]` |
| label | 浠讳綍瀛楃涓 |
| linespacing | 娴偣 |
| multialignment | `['left' / 'right' / 'center' ]` |
| name or fontname | 瀛楃涓诧紝渚嬪 `['Sans' / 'Courier' / 'Helvetica' ...]` |
| picker | [`None` / 娴偣 / 甯冨皵鍊 / 鍙皟鐢ㄥ璞` |
| position | `(x,y)` |
| rotation | [ 瑙掑害鍒剁殑瑙掑害 / 'vertical' / 'horizontal' |
| size or fontsize | [ 鐐圭殑灏哄 | 鐩稿灏哄锛屼緥濡 `['smaller', 'x-large' ]` |
| style or fontstyle | `[ 'normal' / 'italic' / 'oblique']` |
| text | 瀛楃涓叉垨浠讳綍鍙娇鐢╜'%s'`鎵撳嵃鐨勪笢瑗 |
| transform | `matplotlib.transform` 瀹炰緥 |
| variant | `[ 'normal' / 'small-caps' ]` |
| verticalalignment or va | `[ 'center' / 'top' / 'bottom' / 'baseline' ]` |
| visible | `[True / False]` |
| weight or fontweight | `[ 'normal' / 'bold' / 'heavy' / 'light' / 'ultrabold' / 'ultralight']` |
| x | 娴偣 |
| y | 娴偣 |
| zorder | 浠绘剰鏁板 |
浣犲彲浠ヤ娇鐢ㄥ榻愬弬鏁癭horizontalalignment`锛宍verticalalignment`鍜宍multialignment`鏉ュ竷缃枃鏈 `horizontalalignment`鎺у埗鏂囨湰鐨刞x`浣嶇疆鍙傛暟琛ㄧず鏂囨湰杈圭晫妗嗙殑宸﹁竟锛屼腑闂存垨鍙宠竟銆 `verticalalignment`鎺у埗鏂囨湰鐨刞y`浣嶇疆鍙傛暟琛ㄧず鏂囨湰杈圭晫妗嗙殑搴曢儴锛屼腑蹇冩垨椤堕儴銆 `multialignment`锛屼粎瀵逛簬鎹㈣绗﹀垎闅旂殑瀛楃涓诧紝鎺у埗涓嶅悓鐨勮鏄乏锛屼腑杩樻槸鍙冲榻愩 杩欓噷鏄竴涓娇鐢╜text()`鍛戒护鏄剧ず鍚勭瀵归綈鏂瑰紡鐨勪緥瀛愩 鍦ㄦ暣涓唬鐮佷腑浣跨敤`transform = ax.transAxes`锛岃〃绀哄潗鏍囩浉瀵逛簬杞磋竟鐣屾缁欏嚭锛屽叾涓璥0,0`鏄酱鐨勫乏涓嬭锛宍1,1`鏄彸涓婅銆
```py
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
# axes coordinates are 0,0 is bottom left and 1,1 is upper right
p = patches.Rectangle(
(left, bottom), width, height,
fill=False, transform=ax.transAxes, clip_on=False
)
ax.add_patch(p)
ax.text(left, bottom, 'left top',
horizontalalignment='left',
verticalalignment='top',
transform=ax.transAxes)
ax.text(left, bottom, 'left bottom',
horizontalalignment='left',
verticalalignment='bottom',
transform=ax.transAxes)
ax.text(right, top, 'right bottom',
horizontalalignment='right',
verticalalignment='bottom',
transform=ax.transAxes)
ax.text(right, top, 'right top',
horizontalalignment='right',
verticalalignment='top',
transform=ax.transAxes)
ax.text(right, bottom, 'center top',
horizontalalignment='center',
verticalalignment='top',
transform=ax.transAxes)
ax.text(left, 0.5*(bottom+top), 'right center',
horizontalalignment='right',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(left, 0.5*(bottom+top), 'left center',
horizontalalignment='left',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
horizontalalignment='center',
verticalalignment='center',
fontsize=20, color='red',
transform=ax.transAxes)
ax.text(right, 0.5*(bottom+top), 'centered',
horizontalalignment='center',
verticalalignment='center',
rotation='vertical',
transform=ax.transAxes)
ax.text(left, top, 'rotated\nwith newlines',
horizontalalignment='center',
verticalalignment='center',
rotation=45,
transform=ax.transAxes)