使用 matplotlib 用圆圈夹住 2 个或更多三角形
clip 2 or more triangles with a circle using matplotlib
我的问题与this question非常相似!但我想用1个圆夹2个或更多的三角形。
所以我尝试用 add_patch
添加 2 个三角形,但它抛出语法错误:
patchpoly = ax.add_patch(polygon1, polygon2)
所以我知道我不能走这条路。
代码如下:
fig, ax = plt.subplots()
polygon1 = plt.Polygon([(0,0.6),(1,2),(2,0.4)],color='g',alpha=0.25)
polygon2 = plt.Polygon([(0,0),(1,0.5),(0.5,1)],color='b',alpha=0.25)
circle = plt.Circle((0,0), 1.0, color='r', alpha=0.25)
patchpoly = ax.add_patch(polygon1) # i know this line would be meaningless
patchpoly = ax.add_patch(polygon2)
ax.add_patch(circle)
patchpoly.set_clip_path(circle)
好吧,你快到了;只需给 polygon2
补丁一个唯一的名称,即 patchpoly2 = ax.add_patch(polygon2)
然后还指示补丁通过 patchpoly2.set_clip_path(circle)
在圆圈的末尾获得 'clipped'
我的问题与this question非常相似!但我想用1个圆夹2个或更多的三角形。
所以我尝试用 add_patch
添加 2 个三角形,但它抛出语法错误:
patchpoly = ax.add_patch(polygon1, polygon2)
所以我知道我不能走这条路。
代码如下:
fig, ax = plt.subplots()
polygon1 = plt.Polygon([(0,0.6),(1,2),(2,0.4)],color='g',alpha=0.25)
polygon2 = plt.Polygon([(0,0),(1,0.5),(0.5,1)],color='b',alpha=0.25)
circle = plt.Circle((0,0), 1.0, color='r', alpha=0.25)
patchpoly = ax.add_patch(polygon1) # i know this line would be meaningless
patchpoly = ax.add_patch(polygon2)
ax.add_patch(circle)
patchpoly.set_clip_path(circle)
好吧,你快到了;只需给 polygon2
补丁一个唯一的名称,即 patchpoly2 = ax.add_patch(polygon2)
然后还指示补丁通过 patchpoly2.set_clip_path(circle)