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.
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).
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.
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).
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)".
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.


0 Comments:
Post a Comment