PATH
对象的创建通过matplotlib.path.Path(verts,codes)
创建,其中:codes
:指示如何使用这些PATH
顶点。它与verts
关系是一一对应的。有如下指令:Path.STOP
:结束path
的标记Path.MOVETO
:画笔提起并移动到指定的顶点Path.LINETO
:画笔画直线,从current position
到指定的顶点Path.CURVE3
:画笔画二阶贝塞尔曲线,从current position
到指定的end point
, 其中还有一个参数是指定的control point
Path.CURVE4
:画笔画三阶贝塞尔曲线,从current position
到指定的end point
, 其中还有两个参数是指定的control points
Path.CLOSEPOLY
:指定的point
参数被忽略。该指令画一条线段, 从current point
到start point
可以通过来构建一个
PathPatch
对象,然后通过Axes.add_patch(patch)
向Axes
添加PathPatch
对象.这样就添加了Path
到图表中。在
matplotlib
中所有简单的patch primitive
,如Rectangle
、Circle
、Polygon
等等,都是由简单的Path
来实现的。而创建大量的primitive
的函数如hist()
和bar()
(他们创建了大量的Rectanle
)可以使用一个compound path
来高效地实现。...
codes = np.ones(nverts, int) * Path.LINETO
## 设置 codes :codes分成5个一组,
## 每一组以Path.MOVETO开始,后面是3个Path.LINETO,最后是Path.CLOSEPOLY
codes[0::5] = Path.MOVETO
codes[4::5] = Path.CLOSEPOLY
## 设置顶点 verts ##
## 创建 Path 、PathPatch并添加 ##
barpath = Path(verts, codes)
patch = patches.PathPatch(barpath, facecolor='green',edgecolor='yellow', alpha=0.5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(patch)
在创建
Axes
或者SubPlot
时,可以给构造函数提供一个参数来指定背景色