import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
r_max=5
h=10
num_points=100
r=np.linspace(0,r_max,num_points)
theta=np.linspace(0,2*np.pi,num_points)
R,Theta=np.meshgrid(r,theta)
X=R*np.cos(Theta)
Y=R*np.sin(Theta)
Z=h*(1-R/r_max)
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,Z,cmap='inferno',edgecolor='k',alpha=0.7)
ax.set_title('3D conical Pattern')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: This library is used for numerical
computations and working with arrays. In this code, it is used to create arrays
for the radial distance (r) and the angle (theta), as well as to handle the
parametric equations for the cone's surface.
r_max = 5
h = 10
num_points = 100
r_max: This defines the maximum radius of the cone's
base. The cone will have a maximum radius of 5 units at the base.
r = np.linspace(0, r_max, num_points)
theta = np.linspace(0, 2 * np.pi, num_points)
R, Theta = np.meshgrid(r, theta)
r = np.linspace(0, r_max, num_points): This creates
a 1D array of num_points equally spaced values between 0 and r_max (which is
5). These represent the radial distances from the center of the cone.
2ฯ (360 degrees), representing the angle around the
center of the cone.
X = R * np.cos(Theta)
Y = R * np.sin(Theta)
Z = h * (1 - R / r_max)
X = R * np.cos(Theta): Using polar-to-Cartesian
conversion, the X coordinate is calculated by multiplying the radial distance R
by the cosine of the angle Theta.
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
fig = plt.figure(figsize=(10, 8)): This creates a
new figure for the plot with a specified size of 10x8 inches.
ax.plot_surface(X, Y, Z, cmap='inferno',
edgecolor='k', alpha=0.7)
ax.plot_surface(X, Y, Z, cmap='inferno',
edgecolor='k', alpha=0.7): This function plots the surface of the cone using
the X, Y, and Z coordinates calculated earlier. The parameters used are:
ax.set_title('3D Conical Surface')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Conical Surface'): This sets the
title of the plot to "3D Conical Surface".
plt.show()
plt.show(): This function displays the plot. It
renders the figure with the cone surface and allows you to view the result.


0 Comments:
Post a Comment