Monday, 28 April 2025

3D Surface of Revolution Paraboloid using Python


import matplotlib.pyplot as plt

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

def f(x):

    return x**2

x=np.linspace(-3,3,100)

theta=np.linspace(0,2*np.pi,100)

X,Theta=np.meshgrid(x,theta)

Y=f(X)*np.cos(Theta)

Z=f(X)*np.sin(Theta)

fig=plt.figure(figsize=(6,6))

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

ax.plot_surface(X,Y,Z,cmap='inferno',edgecolor='none',alpha=0.7)

ax.set_title('3D Surface of Revolution')

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: A numerical library that provides efficient ways to work with arrays and perform mathematical operations.

 matplotlib.pyplot: A plotting library used for creating various types of graphs and plots. It is commonly used for 2D plotting.

 mpl_toolkits.mplot3d: This is a module from Matplotlib that provides tools for 3D plotting. Specifically, Axes3D is used to create 3D plots.

 2. Define the Function for the Paraboloid

def f(x):

    return x ** 2

f(x): This defines a function that calculates the square of the input x, creating a simple parabolic shape when plotted in 2D.

 3. Create the Data for the Plot

x = np.linspace(-3, 3, 100)  # Generate 100 equally spaced values between -3 and 3

theta = np.linspace(0, 2 * np.pi, 100)  # Generate 100 equally spaced values from 0 to 2ฯ€ (for full revolution)

X, Theta = np.meshgrid(x, theta)  # Create a meshgrid from the x and theta values

x = np.linspace(-3, 3, 100): This generates 100 evenly spaced values between -3 and 3. These represent the x-coordinates for the paraboloid.

 theta = np.linspace(0, 2 * np.pi, 100): This generates 100 evenly spaced values between 0 and 2ฯ€, which represent angles for a full revolution around the Z-axis.

 X, Theta = np.meshgrid(x, theta): np.meshgrid creates a 2D grid of x and theta values. It returns two 2D arrays, X and Theta, that correspond to the coordinates of a grid in the 3D space.

 4. Calculate the Coordinates for the 3D Surface

Y = f(X) * np.cos(Theta)  # Parametric equation for the Y coordinate (scaled by cosine of theta)

Z = f(X) * np.sin(Theta)  # Parametric equation for the Z coordinate (scaled by sine of theta)

Y = f(X) * np.cos(Theta): This calculates the Y-coordinates of the surface. The f(X) part gives the value of the paraboloid function (square of X), and multiplying by cos(Theta) rotates the paraboloid in the Y-axis direction.

 Z = f(X) * np.sin(Theta): Similarly, this calculates the Z-coordinates of the surface. It also uses the parabolic function f(X) and rotates the result around the Z-axis using sin(Theta).

 5. Set Up the Plot

fig = plt.figure(figsize=(10, 8))  # Create a figure with a size of 10x8 inches

ax = fig.add_subplot(111, projection='3d')  # Add a 3D subplot to the figure

fig = plt.figure(figsize=(10, 8)): Initializes a new figure with a specified size of 10 inches by 8 inches.

 ax = fig.add_subplot(111, projection='3d'): Creates a 3D subplot in the figure (111 means a single subplot), with projection='3d' enabling 3D plotting capabilities.

 6. Plot the Surface

ax.plot_surface(X, Y, Z, cmap='inferno', edgecolor='none', alpha=0.7)  # Plot the 3D surface

ax.plot_surface(X, Y, Z, cmap='inferno', edgecolor='none', alpha=0.7):

 This creates the 3D surface plot using the X, Y, and Z coordinates calculated earlier.

 cmap='inferno': Specifies the colormap to use for coloring the surface. inferno is a popular colormap that ranges from dark purple to bright yellow.

 edgecolor='none': Ensures that there are no lines around the edges of the surface.

 alpha=0.7: Adjusts the transparency of the surface to 70% (where 1 is fully opaque and 0 is fully transparent).

 7. Label the Axes and Set the Title

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_title("3D Surface of Revolution (Paraboloid)")  # Set the title of the plot

ax.set_xlabel("X axis"), ax.set_ylabel("Y axis"), ax.set_zlabel("Z axis"): Labels the X, Y, and Z axes, making the plot easier to interpret.

 ax.set_title("3D Surface of Revolution (Paraboloid)"): Adds a title to the plot indicating that this is a surface of revolution, specifically a paraboloid.

 8. Display the Plot

plt.show()  # Show the plot

plt.show(): This line displays the figure. It renders the 3D surface plot on the screen.

 


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)