Sunday, 2 March 2025

Concentric circle plot using python




import numpy as np
import matplotlib.pyplot as plt

fig,ax=plt.subplots(figsize=(6,6))
num_circles=10
radii=np.linspace(0.2,2,num_circles)
for r in radii:
    circle=plt.Circle((0,0),r,color='b',fill=False,linewidth=2)
    ax.add_patch(circle)

ax.set_xlim(-2.5,2.5)
ax.set_ylim(-2.5,2.5)
ax.set_aspect('equal')
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.title("Concentric circle plot")
plt.show()

#source code --> clcoding.com 

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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)