Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove "exterior" parts of a diagonal line that is clipping a circle

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()

enter image description here

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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()

The output:
enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading