Monday, 21 April 2025

3D Conical Surface using Python

 


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.

 matplotlib.pyplot: This is a plotting library that is used for creating static, animated, and interactive visualizations in Python. It’s commonly used for generating 2D plots and charts. Here, it is used to create a 3D plot of the cone surface.

 mpl_toolkits.mplot3d.Axes3D: This is a toolkit for creating 3D plots in matplotlib. It provides the functionality for 3D plotting (e.g., plot_surface) and visualization in 3D space.

 2. Defining Parameters for the Cone

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.

 h: This represents the height of the cone, which is set to 10 units. It defines how tall the cone will be.

 num_points: This is the number of points used to discretize the radial distance (r) and angular coordinates (theta). More points give higher resolution in the plot. Here, it is set to 100.

 3. Creating the Radial and Angular Coordinates

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.

 theta = np.linspace(0, 2 * np.pi, num_points): This generates num_points equally spaced values between 0 and

2ฯ€ (360 degrees), representing the angle around the center of the cone.

 R, Theta = np.meshgrid(r, theta): This generates two 2D arrays (R and Theta) from the 1D arrays r and theta. The R array represents the radial distances for every value of theta, and the Theta array represents the angular values for every value of r. These 2D arrays are the grid of coordinates used to define the surface of the cone.

 4. Parametric Equations for the Cone Surface

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.

 Y = R * np.sin(Theta): Similarly, the Y coordinate is calculated by multiplying the radial distance R by the sine of the angle Theta.

 Z = h * (1 - R / r_max): The Z coordinate represents the height of each point on the cone's surface. At the apex of the cone (where R = 0), the height Z is equal to h (the cone's height). As R increases toward r_max, the height decreases linearly. This gives the cone its tapered shape.

 5. Creating the 3D Plot

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 = fig.add_subplot(111, projection='3d'): This adds a 3D subplot to the figure, meaning the plot will be created in 3D space. The 111 argument specifies a 1x1 grid and places the subplot in the first position.

 6. Plotting the Cone Surface

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:

 cmap='inferno': This specifies the color map to use for the surface. The 'inferno' colormap gives the surface a warm color gradient (yellow to red to dark).

 edgecolor='k': This sets the color of the edges of the surface to black ('k' stands for black).

 alpha=0.7: This sets the transparency of the surface to 70%. The surface will be partially transparent, which allows for better visualization of underlying structures if needed.

 7. Customizing the Plot

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

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

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

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

 8. Displaying the Plot

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

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)