Wednesday, 16 April 2025

3D Parametric Helix Pattern using Python

 


import matplotlib.pyplot as plt

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

n=1000

t=np.linspace(0,10*np.pi,n)

x=np.sin(t)

y=np.cos(t)

z=t

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

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

ax.plot(x,y,z,color='purple',lw=2)

ax.set_title("3D Parametric Helix Pattern",fontsize=16)

ax.set_xlabel('X axis')

ax.set_xlabel('Y axis')

ax.set_xlabel('Z axis')

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Import Libraries

 import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np: Imports the numpy library, which is used for numerical calculations, such as generating the spiral coordinates.

import matplotlib.pyplot as plt: Imports matplotlib, which is used for plotting 2D and 3D graphs.

from mpl_toolkits.mplot3d import Axes3D: Imports the Axes3D module from mpl_toolkits, which enables 3D plotting in matplotlib.

 2. Set Number of Points for the Spiral

n = 1000

n = 1000: Defines the number of points (or "steps") you want along the helix. The higher the value of n, the smoother the curve will appear because there will be more data points in the plot.

 3. Create Parametric Equation for the Helix

t = np.linspace(0, 10 * np.pi, n) 

x = np.sin(t)

y = np.cos(t) 

z = t 

t = np.linspace(0, 10 * np.pi, n):

 np.linspace(0, 10 * np.pi, n) generates n evenly spaced values of t between 0 and 10ฯ€. The t values represent the angle (or "time") along the helix as it twists. This range will make the helix loop several times.

 x = np.sin(t):

 The x coordinates of the helix are given by the sine of t. This creates a horizontal oscillation in the X direction (wave-like movement).

 y = np.cos(t):

 The y coordinates are given by the cosine of t. This works alongside the sine to create a circular motion around the Z-axis, giving the 3D spiral its shape.

 z = t:

 The z coordinates simply follow the values of t, which means the helix rises (or spirals) upward as t increases. The Z-axis is the height of the spiral.

 4. Set Up the Plot

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

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

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

 Creates a new figure for the plot. The figsize argument defines the dimensions of the plot (10 inches wide and 6 inches tall).

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

 Adds a 3D subplot to the figure. The 111 means "1 row, 1 column, 1st subplot" (in this case, there's only one plot).

 projection='3d' tells matplotlib that you want to create a 3D plot.

 5. Plot the Helix

ax.plot(x, y, z, color='purple', lw=2)

ax.plot(x, y, z, color='purple', lw=2):

 This function plots the x, y, and z coordinates in 3D space.

 color='purple': Sets the color of the helix line to purple.

 lw=2: Sets the line width to 2 (makes the line a little thicker, which makes it stand out more).

 6. Set Titles and Labels for the Axes

ax.set_title('3D Parametric Helix (DNA Spiral)', fontsize=16)

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_title('3D Parametric Helix (DNA Spiral)', fontsize=16):

 Adds a title to the plot: "3D Parametric Helix (DNA Spiral)", and sets the font size to 16.

 ax.set_xlabel('X axis'): Labels the X-axis as "X axis".

 ax.set_ylabel('Y axis'): Labels the Y-axis as "Y axis".

 ax.set_zlabel('Z axis'): Labels the Z-axis as "Z axis".

 These labels are displayed on the plot to indicate which axis corresponds to which dimension in the 3D space.

 7. Display the Plot

plt.show()

plt.show(): Displays the plot. This is the final step that actually renders the 3D helix and opens it in a plotting window.

 


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)