Sunday, 27 April 2025

3D Concentric Circle Parametric using Python

 


import matplotlib.pyplot as plt

import numpy as np

num_circles=10

radius_step=0.5

z_step=1

t=np.linspace(0,2*np.pi,100)

fig=plt.figure(figsize=(6,6))

ax=fig.add_subplot(111,projection='3d')

for i in range(num_circles):

    r=(i+1)*radius_step

    z_layer=i*z_step

    x=r*np.cos(t)

    y=r*np.sin(t)

    z=np.full_like(t,z_layer)

    ax.plot(x,y,z,label=f'Circle {i+1}')

ax.set_title('3D Concetric Circle')

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_box_aspect([1,1,2])

plt.tight_layout()

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: A library for numerical computations, especially for handling arrays and performing mathematical operations. In this case, it's used for generating a range of values (t), creating arrays for X, Y, and Z coordinates, and performing mathematical operations like cos and sin.

 matplotlib.pyplot: A library used for creating visualizations in Python. The pyplot module is a simple interface for creating various types of plots, including 3D plots.

 2. Define Parameters for the Plot

num_circles = 10  # Number of concentric circles

radius_step = 0.5  # Step size for radius of each circle

z_step = 1  # Step size for the Z-coordinate of each circle

t = np.linspace(0, 2 * np.pi, 100)  # Create a set of 100 points from 0 to 2ฯ€ for parametric circle

num_circles: Specifies the number of concentric circles to plot (in this case, 10 circles).

 radius_step: This defines how much the radius of each successive circle increases. The first circle has a radius of 0.5, the second one has 1.0, and so on.

 z_step: Controls how much the Z-coordinate (height) increases for each successive circle. This step is 1, so each circle is placed one unit higher than the previous one on the Z-axis.

 t = np.linspace(0, 2 * np.pi, 100): Generates 100 evenly spaced values between 0 and 2ฯ€. These values will be used to parametrize the circle in the X-Y plane using cosine and sine functions.

 3. Set Up the Plot

 fig = plt.figure(figsize=(6, 6))  # Create a figure with a specific size

ax = fig.add_subplot(111, projection='3d')  # Create a 3D axis for the plot

fig = plt.figure(figsize=(6, 6)): Initializes a figure for plotting with a specified size of 6 inches by 6 inches.

 ax = fig.add_subplot(111, projection='3d'): Adds a 3D subplot to the figure. The argument 111 means there is only one subplot, and projection='3d' ensures that the plot will be 3D.

 4. Loop to Draw Concentric Circles

for i in range(num_circles):  # Loop over the number of circles

    r = (i + 1) * radius_step  # Calculate the radius for the current circle

    z_layer = i * z_step  # Calculate the Z position for the current circle

    x = r * np.cos(t)  # X-coordinates of the circle using the parametric equation

    y = r * np.sin(t)  # Y-coordinates of the circle using the parametric equation

    z = np.full_like(t, z_layer)  # Create an array of Z values for the circle (constant)

    ax.plot(x, y, z, label=f'Circle {i + 1}')  # Plot the current circle with label

for i in range(num_circles):: A loop that runs from i = 0 to i = num_circles - 1 (in this case, 10 iterations for 10 circles).

 r = (i + 1) * radius_step: The radius of the current circle is calculated by multiplying (i + 1) by radius_step. This ensures the radius increases as we move through the loop.

 z_layer = i * z_step: The Z-coordinate (height) of the current circle is calculated by multiplying i by z_step. This places each circle higher on the Z-axis by one unit.

 x = r * np.cos(t): The X-coordinate is calculated using the parametric equation for a circle, where r is the radius and t is the angle between 0 and 2ฯ€. cos(t) gives the X position for each point on the circle.

 y = r * np.sin(t): Similarly, the Y-coordinate is calculated using sin(t) for each value in t.

 z = np.full_like(t, z_layer): The Z-coordinate for all points of the circle is the same (z_layer), ensuring the circle lies flat at a constant height.

 ax.plot(x, y, z, label=f'Circle {i + 1}'): This line actually plots the circle using the x, y, and z values. The label is used to identify the circle in the plot's legend.

 5. Set Titles and Labels

ax.set_title("3D Concentric Circles (Parametric)")  # Set the title of the plot

ax.set_xlabel("X axis")  # Label the X-axis

ax.set_ylabel("Y axis")  # Label the Y-axis

ax.set_zlabel("Z axis")  # Label the Z-axis

ax.set_box_aspect([1, 1, 2])  # Adjust the aspect ratio of the plot (stretch the Z-axis)

ax.set_title("3D Concentric Circles (Parametric)"): Sets the title of the 3D plot to "3D Concentric Circles (Parametric)".

 ax.set_xlabel("X axis"), ax.set_ylabel("Y axis"), ax.set_zlabel("Z axis"): Labels the X, Y, and Z axes of the 3D plot.

 ax.set_box_aspect([1, 1, 2]): Adjusts the aspect ratio of the plot. In this case, the Z-axis is stretched twice as much as the X and Y axes to give a better view of the concentric circles in 3D.

 6. Final Layout Adjustment and Plot Display

plt.tight_layout()  # Adjust the layout to prevent clipping

plt.show()  # Display the plot

plt.tight_layout(): Automatically adjusts the layout of the plot to make sure everything fits nicely within the figure.

 plt.show(): Displays the plot. This is the final step that renders the figure on the screen.

 

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)