P. Vauclin
P. Vauclin

Reputation: 387

matplotlib hide some part of the rectangle

I'm trying to hide or delete the part of "outer_rect" that goes beyond "outer_arc_left" and "outer_arc_right" in order to recreate an ice rink.

Thank you.

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

Upvotes: 1

Views: 548

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

It's hard to "hide part of a patch" if there is not clear definition of what part to hide. You can create a path and use it as clip_path but that acts more like a mask, and e.g. the edges of the rectangle would be cropped. Defining that path would then be the tricky bit.

But we can inverse this: If you anyways need to define a path, you could simply use that path as the shape you want to show.

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

Upvotes: 1

Related Questions