绘制甜甜圈
Draw donuts (miam!) using Paths and PathPatches. This example shows the effect of the path’s orientations in a compound path.
import numpy as npimport matplotlib.path as mpathimport matplotlib.patches as mpatchesimport matplotlib.pyplot as pltdef wise(v):if v == 1:return "CCW"else:return "CW"def make_circle(r):t = np.arange(0, np.pi * 2.0, 0.01)t = t.reshape((len(t), 1))x = r * np.cos(t)y = r * np.sin(t)return np.hstack((x, y))Path = mpath.Pathfig, ax = plt.subplots()inside_vertices = make_circle(0.5)outside_vertices = make_circle(1.0)codes = np.ones(len(inside_vertices), dtype=mpath.Path.code_type) * mpath.Path.LINETOcodes[0] = mpath.Path.MOVETOfor i, (inside, outside) in enumerate(((1, 1), (1, -1), (-1, 1), (-1, -1))):# Concatenate the inside and outside subpaths together, changing their# order as neededvertices = np.concatenate((outside_vertices[::outside],inside_vertices[::inside]))# Shift the pathvertices[:, 0] += i * 2.5# The codes will be all "LINETO" commands, except for "MOVETO"s at the# beginning of each subpathall_codes = np.concatenate((codes, codes))# Create the Path objectpath = mpath.Path(vertices, all_codes)# Add plot itpatch = mpatches.PathPatch(path, facecolor='#885500', edgecolor='black')ax.add_patch(patch)ax.annotate("Outside %s,\nInside %s" % (wise(outside), wise(inside)),(i * 2.5, -1.5), va="top", ha="center")ax.set_xlim(-2, 10)ax.set_ylim(-3, 2)ax.set_title('Mmm, donuts!')ax.set_aspect(1.0)plt.show()

参考
此示例中显示了以下函数,方法,类和模块的使用:
import matplotlibmatplotlib.pathmatplotlib.path.Pathmatplotlib.patchesmatplotlib.patches.PathPatchmatplotlib.patches.Circlematplotlib.axes.Axes.add_patchmatplotlib.axes.Axes.annotatematplotlib.axes.Axes.set_aspectmatplotlib.axes.Axes.set_xlimmatplotlib.axes.Axes.set_ylimmatplotlib.axes.Axes.set_title
