10分钟
Path
3. 在matplotlib中所有简单的patch primitive,如Rectangle、Circle、Polygon等等,都是由简单的Path来实现的。而创建大量的primitive的函数如hist()和bar()(他们创建了大量的Rectanle)可以使用一个compound path来高效地实现。 但是实际上bar()创建的是一系列的Rectangle,而没有用到compound path,这是由于历史原因,是历史遗留问题。(bar()函数先于Coupound Path出现) 下面是一个Compound Path的例子:
...
verts = np.zeros((nverts, 2)) # nverts为顶点的个数加1(一个终止符)
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)
ax.show()
在创建Axes或者SubPlot时,可以给构造函数提供一个axisbg参数来指定背景色
学员评价