1. 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 pointstart point

      可以通过来构建一个PathPatch对象,然后通过Axes.add_patch(patch)Axes添加PathPatch对象.这样就添加了Path到图表中。

    2. matplotlib中所有简单的patch primitive,如RectangleCirclePolygon等等,都是由简单的Path来实现的。而创建大量的primitive的函数如hist()bar()(他们创建了大量的Rectanle)可以使用一个compound path来高效地实现。

      1. ...
      2. codes = np.ones(nverts, int) * Path.LINETO
      3. ## 设置 codes :codes分成5个一组,
      4. ## 每一组以Path.MOVETO开始,后面是3个Path.LINETO,最后是Path.CLOSEPOLY
      5. codes[0::5] = Path.MOVETO
      6. codes[4::5] = Path.CLOSEPOLY
      7. ## 设置顶点 verts ##
      8. ## 创建 Path 、PathPatch并添加 ##
      9. barpath = Path(verts, codes)
      10. patch = patches.PathPatch(barpath, facecolor='green',edgecolor='yellow', alpha=0.5)
      11. fig = plt.figure()
      12. ax = fig.add_subplot(111)
      13. ax.add_patch(patch)

      在创建Axes或者SubPlot时,可以给构造函数提供一个参数来指定背景色