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