In this answer it shown how to use transdata to clip a circle with a diagonal line. The code and images are:
import matplotlib.pyplot as plt
# Create the circle with radius 6
circle = plt.Circle((0, 0), 6, color='r', fill=False)
# Set up the plot (reuse the previous grid settings)
plt.figure(figsize=(8, 8))
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid()
# Add the circle to the plot
ax = plt.gca()
ax.add_patch(circle)
# Draw a diagonal line
plt.plot([0, 7], [7, 0], color='b', linestyle='--')
# Set aspect ratio to ensure square grid cells
ax.set_aspect("equal")
polygon = plt.Polygon([[0, 0], [7, 0], [0, 7]], transform=ax.transData)
# Clip the circle using the diagonal line.
circle.set_clip_path(polygon)
# Show the plot
plt.title("Circle Centered at (0,0) Clipped by Diagonal Line")
plt.show()
I would like the same image but with the part of the diagonal line outside the circle remmove. So all that is left is the truncate circular arc.
>Solution :
You could make the triangular polygon slightly bigger, style it appropriately, and add the respective patch to the plot instead of drawing a special diagonal line. Then you could add the circle, clip it using the polygon, and finally clip the polygon too, using the circle:
import matplotlib.pyplot as plt
# Create the circle with radius 6
circle = plt.Circle((0, 0), 6, color='r', fill=False)
# Set up the plot (reuse the previous grid settings)
plt.figure(figsize=(8, 8))
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid()
# Add the circle to the plot
ax = plt.gca()
# Set aspect ratio to ensure square grid cells
ax.set_aspect("equal")
# Make a polygon looking like a diagonal line on the visible part of the plot
polygon = plt.Polygon([[-1, -1], [7, 0], [0, 7]], transform=ax.transData, edgecolor='b', facecolor='w', linestyle='--')
# Add the polygon and circle to the plot
ax.add_patch(polygon)
ax.add_patch(circle)
# Clip the circle using the polygon appearing as a "diagonal line"
circle.set_clip_path(polygon)
# Clip the polygon ("exterior parts of the diagonal line") using the circle
polygon.set_clip_path(circle)
# Show the plot
plt.title("Circle Centered at (0,0) Clipped by Diagonal Line")
plt.show()

