matplotlib隐藏矩形的某些部分

时间:2019-03-17 12:10:23

标签: python matplotlib figure

我试图隐藏或删除超出“ outer_arc_left”和“ outer_arc_right”的“ outer_rect”部分,以重新创建溜冰场。

谢谢。

from matplotlib.patches import Rectangle, Arc

def draw_rink(ax=None, color='black', lw=2, outer_lines=False):
    if ax is None:
        ax = plt.gca()
    outer_rect = Rectangle((-1500, 3000), 3000, 3000, linewidth=lw, color=color, fill=False)
    outer_arc_left = Arc((-700, 5200), 1600, 1600, theta1=90, theta2=180, linewidth=lw, color=color)
    outer_arc_right = Arc((700, 5200), 1600, 1600, theta1=0, theta2=90, linewidth=lw, color=color)
    rink_elements = [outer_rect, outer_arc_left, outer_arc_right]
    for element in rink_elements:
        ax.add_patch(element)
    return ax

1 个答案:

答案 0 :(得分:1)

如果不清楚要隐藏的部分,则很难“隐藏补丁的一部分”。您可以创建路径并将其用作clip_path,但其作用更像是屏蔽,例如矩形的边缘将被裁剪。定义该路径将是棘手的事情。

但是我们可以反过来:如果仍然需要定义路径,则可以简单地使用该路径作为要显示的形状。

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

x = [1,1,1,2,3,4,4,4,1]
y = [0,1,2,2,2,2,1,0,0]
verts = list(zip(x,y))
codes = [1,2,3,3,2,3,3,2,2]
path = Path(verts,codes)
patch = PathPatch(path)
plt.gca().add_patch(patch)

plt.gca().autoscale()
plt.show()

enter image description here