File indexing completed on 2026-04-09 07:48:49
0001
0002 """
0003 https://matplotlib.org/stable/gallery/shapes_and_collections/donut.html#sphx-glr-gallery-shapes-and-collections-donut-py
0004
0005
0006 https://stackoverflow.com/questions/44025403/how-to-use-matplotlib-path-to-draw-polygon
0007
0008 """
0009 import numpy as np
0010 import matplotlib.path as mpath
0011 import matplotlib.patches as mpatches
0012 import matplotlib.pyplot as plt
0013
0014 def wise(v):
0015 return "CCW" if v == 1 else "CW"
0016
0017 def make_circle(r):
0018 t = np.arange(0, np.pi * 2.0, 0.01)
0019 t = t.reshape((len(t), 1))
0020 x = r * np.cos(t)
0021 y = r * np.sin(t)
0022 return np.hstack((x, y))
0023
0024 Path = mpath.Path
0025
0026 fig, ax = plt.subplots()
0027
0028 inside_vertices = make_circle(0.5)
0029 outside_vertices = make_circle(1.0)
0030
0031 codes = np.ones(len(inside_vertices), dtype=mpath.Path.code_type) * mpath.Path.LINETO
0032 codes[0] = mpath.Path.MOVETO
0033
0034
0035
0036
0037
0038 spec = ((-1, 1),)
0039
0040
0041 for i, (inside, outside) in enumerate(spec):
0042
0043 vertices = np.concatenate((outside_vertices[::outside],inside_vertices[::inside]))
0044 vertices[:, 0] += i * 2.5
0045 all_codes = np.concatenate((codes, codes))
0046 path = mpath.Path(vertices, all_codes)
0047 patch = mpatches.PathPatch(path, facecolor='#885500', edgecolor='black')
0048 ax.add_patch(patch)
0049 ax.annotate("Outside %s,\nInside %s" % (wise(outside), wise(inside)),(i * 2.5, -1.5), va="top", ha="center")
0050 pass
0051
0052 ax.set_xlim(-2, 2.5*len(spec))
0053 ax.set_ylim(-3, 2)
0054 ax.set_title('Mmm, donuts!')
0055 ax.set_aspect(1.0)
0056 plt.show()
0057
0058