Tuesday, 27 May 2025

3D Spiral Staircase Pattern using Python


 import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

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

num_steps = 50         

height_per_step = 0.2  

radius = 2             

theta = np.linspace(0, 4 * np.pi, num_steps)  

x = radius * np.cos(theta)

y = radius * np.sin(theta)

z = height_per_step * np.arange(num_steps)

ax.scatter(x, y, z, c='brown', s=100, label='Steps')

ax.plot([0,0], [0,0], [0, z[-1]+1], color='grey', linestyle='--', linewidth=2, label='Central Pole')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z (height)')

ax.legend()

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Import Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy for numerical operations and array handling.

matplotlib.pyplot for plotting graphs.

 2. Create Figure and 3D Axes

fig = plt.figure()

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

Creates a new plotting figure.

Adds a 3D subplot to the figure (one plot, with 3D projection).

 3. Define Parameters for Staircase

num_steps = 50         # Number of steps in the staircase

height_per_step = 0.2  # Height increase for each step

radius = 2             # Radius of spiral path

Sets how many steps.

How much each step rises vertically.

How far each step is from the center.

 4. Calculate Step Angles

theta = np.linspace(0, 4 * np.pi, num_steps)

Creates num_steps angles evenly spaced from 0 to

4π (two full rotations).

 5. Calculate Step Positions (x, y, z)

x = radius * np.cos(theta)

y = radius * np.sin(theta)

z = height_per_step * np.arange(num_steps)

Computes x, y coordinates on a circle of given radius (using cosine and sine).

 z increases linearly with step number, creating height.

 6. Plot Steps as Points

ax.scatter(x, y, z, c='brown', s=100, label='Steps')

Plots each step as a brown dot sized 100.

Labels them for the legend.

 7. Plot Central Pole

ax.plot([0,0], [0,0], [0, z[-1]+1], color='grey', linestyle='--', linewidth=2, label='Central Pole')

Draws a vertical dashed line at the center representing the staircase pole.

Goes from height 0 to just above the last step.

 8. Set Axis Labels

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z (height)')

Labels each axis to indicate directions.

 9. Add Legend

ax.legend()

Adds a legend explaining plotted elements (steps and pole).

 10. Display the Plot

plt.show()

Shows the final 3D spiral staircase plot.

 

 

 

 

 

 

 

 

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (150) 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 (216) Data Strucures (13) Deep Learning (67) 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 (185) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1215) 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)