3.8

涓婄骇 ab95ecde
# 璺緞鏁欑▼
> 鍘熸枃锛歔Path Tutorial](http://matplotlib.org/users/path_tutorial.html)
> 璇戣咃細[椋為緳](https://github.com/)
> 鍗忚锛歔CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
浣嶄簬鎵鏈`matplotlib.patch`瀵硅薄搴曞眰鐨勫璞℃槸`Path`锛屽畠鏀寔`moveto``lineto``curveto`鍛戒护鐨勬爣鍑嗗嚑涓紝鏉ョ粯鍒剁敱绾挎鍜屾牱鏉$粍鎴愮殑绠鍗曞拰澶嶅悎杞粨銆 璺緞鐢`(x,y)`椤剁偣鐨`(N,2)`鏁扮粍锛屼互鍙婅矾寰勪唬鐮佺殑闀垮害涓 N 鐨勬暟缁勫疄渚嬪寲銆 渚嬪锛屼负浜嗙粯鍒`(0,0)``(1,1)`鐨勫崟浣嶇煩褰紝鎴戜滑鍙互浣跨敤杩欎釜浠g爜锛
```py
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
verts = [
(0., 0.), # left, bottom
(0., 1.), # left, top
(1., 1.), # right, top
(1., 0.), # right, bottom
(0., 0.), # ignored
]
codes = [Path.MOVETO,
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.CLOSEPOLY,
]
path = Path(verts, codes)
fig = plt.figure()
ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='orange', lw=2)
ax.add_patch(patch)
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.show()
```
![](http://matplotlib.org/_images/path_tutorial-1.png)
涓嬮潰鐨勮矾寰勪唬鐮佷細琚帴鍙楋細
| 浠g爜 | 椤剁偣 | 鎻忚堪 |
| --- | --- |
| `STOP` | 1 锛堣蹇界暐锛 | 鏍囧織鏁翠釜璺緞缁堢偣鐨勬爣璁帮紙褰撳墠涓嶉渶瑕佹垨宸插拷鐣ワ級 |
| `MOVETO` | 1 | 鎻愯捣绗斿苟绉诲姩鍒版寚瀹氶《鐐 |
| `LINETO` | 1 | 浠庡綋鍓嶄綅缃悜鎸囧畾椤剁偣鐢荤嚎 |
| `CURVE3` | 2 锛堜竴涓帶鍒剁偣锛屼竴涓粓鐐癸級 | 浠庡綋鍓嶄綅缃紝浠ョ粰瀹氭帶鍒剁偣鍚戠粰瀹氱鐐圭敾璐濆灏旀洸绾 |
| `CURVE4` | 3 锛堜袱涓帶鍒剁偣锛屼竴涓粓鐐癸級 | 浠庡綋鍓嶄綅缃紝浠ョ粰瀹氭帶鍒剁偣鍚戠粰瀹氱鐐圭敾涓夋璐濆灏旀洸绾 |
| `CLOSEPOLY` | 1 锛堢偣鑷韩琚拷鐣ワ級 | 鍚戝綋鍓嶆姌绾跨殑璧风偣鐢荤嚎 |
## 璐濆灏旂ず渚
涓浜涜矾寰勭粍浠堕渶瑕佷互澶氫釜椤剁偣鏉ユ寚瀹氾細渚嬪`CURVE3`鏄叿鏈変竴涓帶鍒剁偣鍜屼竴涓鐐圭殑璐濆灏旀洸绾匡紝`CURVE4`鍏锋湁鐢ㄥ仛涓や釜鎺у埗鐐瑰拰绔偣鐨勪笁涓《鐐广 涓嬮潰鐨勭ず渚嬫樉绀轰簡`CURVE4`璐濆灏旀洸绾 - 璐濆灏旀洸绾垮皢鍖呭惈鍦ㄨ捣濮嬬偣锛屼袱涓帶鍒剁偣鍜岀粓鐐圭殑鍑稿寘涓細
```py
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
verts = [
(0., 0.), # P0
(0.2, 1.), # P1
(1., 0.8), # P2
(0.8, 0.), # P3
]
codes = [Path.MOVETO,
Path.CURVE4,
Path.CURVE4,
Path.CURVE4,
]
path = Path(verts, codes)
fig = plt.figure()
ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)
xs, ys = zip(*verts)
ax.plot(xs, ys, 'x--', lw=2, color='black', ms=10)
ax.text(-0.05, -0.05, 'P0')
ax.text(0.15, 1.05, 'P1')
ax.text(1.05, 0.85, 'P2')
ax.text(0.85, -0.05, 'P3')
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
plt.show()
```
![](http://matplotlib.org/_images/path_tutorial-2.png)
## 澶嶅悎璺緞
鎵鏈夊湪 matplotlib锛孯ectangle锛孋ircle锛孭olygon 绛変腑鐨勭畝鍗曡ˉ涓佸師璇兘鏄敤绠鍗曠殑璺緞瀹炵幇鐨勩傞氳繃浣跨敤澶嶅悎璺緞锛岄氬父鍙互鏇存湁鏁堝湴瀹炵幇缁樺埗鍑芥暟锛屽`hist()``bar()`锛屽畠浠垱寤轰簡璁稿鍘熻锛屼緥濡備竴鍫`Rectangle`锛岄氬父鍙娇鐢ㄥ鍚堣矾寰勬潵瀹炵幇銆`bar`鍒涘缓涓涓煩褰㈠垪琛紝鑰屼笉鏄竴涓鍚堣矾寰勶紝寰堝ぇ绋嬪害涓婂嚭浜庡巻鍙插師鍥狅細璺緞浠g爜鏄瘮杈冩柊鐨勶紝`bar `鍦ㄥ畠涔嬪墠灏卞瓨鍦ㄣ傝櫧鐒舵垜浠幇鍦ㄥ彲浠ユ敼鍙樺畠锛屼絾瀹冧細鐮村潖鏃х殑浠g爜锛屾墍浠ュ鏋滀綘闇瑕佷负浜嗘晥鐜囷紝鍦ㄤ綘鑷繁鐨勪唬鐮佷腑杩欐牱鍋氾紝渚嬪锛屽垱寤哄姩鐢绘潯褰㈠浘锛屽湪杩欓噷鎴戜滑灏嗕粙缁嶅浣曞垱寤哄鍚堣矾寰勶紝鏇挎崲`bar`涓殑鍔熻兘銆
鎴戜滑灏嗛氳繃涓烘瘡涓洿鏂瑰浘鐨勬潯褰㈠垱寤轰竴绯诲垪鐭╁舰锛屾潵鍒涘缓鐩存柟鍥惧浘琛細鐭╁舰瀹藉害鏄潯褰㈢殑瀹藉害锛岀煩褰㈤珮搴︽槸璇ユ潯褰腑鐨勬暟鎹偣鏁伴噺銆傞鍏堬紝鎴戜滑灏嗗垱寤轰竴浜涢殢鏈虹殑姝f佸垎甯冩暟鎹苟璁$畻鐩存柟鍥俱傚洜涓 numpy 杩斿洖鏉″舰杈圭紭鑰屼笉鏄腑蹇冿紝鎵浠ヤ笅闈㈢殑绀轰緥涓`bins`鐨勯暱搴︽瘮`n`鐨勯暱搴﹀ぇ 1锛
```py
# histogram our data with numpy
data = np.random.randn(1000)
n, bins = np.histogram(data, 100)
```
鎴戜滑鐜板湪灏嗘彁鍙栫煩褰㈢殑瑙掋 涓嬮潰鐨勬瘡涓`left``bottom`绛夋暟缁勯暱搴︿负`len(n)`锛屽叾涓`n`鏄瘡涓洿鏂瑰浘鏉″舰鐨勮鏁版暟缁勶細
```py
# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
```
鐜板湪鎴戜滑蹇呴』鏋勯犲鍚堣矾寰勶紝瀹冪敱姣忎釜鐭╁舰鐨勪竴绯诲垪`MOVETO``LINETO``CLOSEPOLY`缁勬垚銆 瀵逛簬姣忎釜鐭╁舰锛屾垜浠渶瑕 5 涓《鐐癸細涓涓唬琛`MOVETO`锛屼笁涓唬琛`LINETO`锛屼竴涓唬琛`CLOSEPOLY`銆 濡備笂琛ㄦ墍绀猴紝`closepoly`鐨勯《鐐硅蹇界暐锛屼絾鎴戜滑浠嶇劧闇瑕佸畠鏉ヤ繚鎸佷唬鐮佷笌椤剁偣瀵归綈锛
```py
nverts = nrects*(1+3+1)
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * path.Path.LINETO
codes[0::5] = path.Path.MOVETO
codes[4::5] = path.Path.CLOSEPOLY
verts[0::5,0] = left
verts[0::5,1] = bottom
verts[1::5,0] = left
verts[1::5,1] = top
verts[2::5,0] = right
verts[2::5,1] = top
verts[3::5,0] = right
verts[3::5,1] = bottom
```
鍓╀笅鐨勫氨鏄垱寤鸿矾寰勪簡锛屽皢鍏舵坊鍔犲埌`PathPatch`锛屽皢鍏舵坊鍔犲埌鎴戜滑鐨勮酱鍩燂細
```py
barpath = path.Path(verts, codes)
patch = patches.PathPatch(barpath, facecolor='green',
edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)
```
缁撴灉涓猴細
![](http://matplotlib.org/_images/compound_path_demo.png)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
鍏堝畬鎴愭娑堟伅鐨勭紪杈戯紒
鎯宠璇勮璇 娉ㄥ唽