Code Explanation:
1. Import Required Libraries
import matplotlib.pyplot as plt
import numpy as np
matplotlib.pyplot: The core library for creating visualizations.
numpy: Used to generate evenly spaced values for the circle radii.
2. Create the Figure and Axis
fig, ax = plt.subplots(figsize=(6, 6))
fig, ax = plt.subplots() creates a figure (fig) and an axis (ax), which we use to draw our circles.
figsize=(6,6) ensures the figure is a square, keeping the circles proportionate.
3. Define the Circles
num_circles = 10 # Number of concentric circles
radii = np.linspace(0.2, 2, num_circles) # Generate 10 radius values between 0.2 and 2
num_circles = 10: We want 10 circles.
np.linspace(0.2, 2, num_circles):
Generates 10 values between 0.2 (smallest circle) and 2 (largest circle).
These values represent the radii of the circles.
4. Draw Each Circle
for r in radii:
circle = plt.Circle((0, 0), r, color='b', fill=False, linewidth=2) # Blue hollow circles
ax.add_patch(circle)
We loop through each radius in radii:
plt.Circle((0, 0), r, color='b', fill=False, linewidth=2):
(0,0): Sets the center at the origin.
r: Defines the radius of the circle.
color='b': Blue (b) outline color.
fill=False: Ensures only the outline is drawn, not a solid circle.
linewidth=2: Sets the thickness of the circle outline.
ax.add_patch(circle): Adds the circle to the plot.
5. Adjust the Plot
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
ax.set_aspect('equal') # Ensures circles are perfectly round
ax.set_xlim(-2.5, 2.5) / ax.set_ylim(-2.5, 2.5):
Expands the plot’s limits slightly beyond the largest circle (radius 2).
ax.set_aspect('equal'):
Prevents distortion by ensuring equal scaling on both axes.
6. Hide Axes for a Clean Look
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_xticks([]) / ax.set_yticks([]): Removes tick marks from the plot.
ax.spines[...]: Hides the border lines around the plot.
7. Display the Plot
plt.show()
plt.show() renders and displays the plot.
0 Comments:
Post a Comment