Issue
This code plots a grid and then draws two circles of different sizes on it.
from math import sqrt
# Calculate the center coordinates and add blue dots
for x in np.arange(0.5, 10, 1):
for y in np.arange(0.5, 10, 1):
plt.scatter(x, y, color='blue', s=10) # Adjust the size (s) as needed
# Draw a circle with center in the top left and radius to touch one of the blue dots
d = 3
circle1 = plt.Circle((0, 0), radius=sqrt(2) * (d + 0.5), color='red', alpha=0.5)
plt.gca().add_patch(circle1)
# Draw the second circle with a different radius
circle2 = plt.Circle((0, 0), radius=sqrt(2) * (d + 1 + 0.5), color='blue', alpha=0.5)
plt.gca().add_patch(circle2)
print(f"radius of smaller circle is {sqrt(2) * (d + 0.5)}")
print(f"radius of larger circle is {sqrt(2) * (d + 1 + 0.5)}")
# Draw a square at (4, 4) with a really thick edge
#square = patches.Rectangle((4, 4), 1, 1, fill=None, edgecolor='green', linewidth=5)
#plt.gca().add_patch(square)
plt.yticks(np.arange(0, 10.01, 1))
plt.xticks(np.arange(0, 10.01, 1))
plt.xlim(0,10)
plt.ylim(0,10)
# Manually set tick positions and labels
plt.gca().invert_yaxis()
# Set aspect ratio to be equal
plt.gca().set_aspect('equal', adjustable='box')
plt.grid()
This gives:
I would like to only show the part that is in the larger circle and not the smaller one. That is, the part in blue. How can I do that?
Solution
What you want is an annulus, which can be created using matplotlib.patches.Annulus
.
from matplotlib.patches import Annulus
# other code but I removed the circles
r1 = sqrt(2) * (d + 0.5)
r2 = sqrt(2) * (d + 1 + 0.5)
annulus1 = Annulus((0, 0), r2, r2 - r1, color="blue", alpha=0.5)
plt.gca().add_patch(annulus1)
# other code for setting ticks and showing the plot
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.