Saturday, 31 May 2025

Step Function Grid using Python


 import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

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

y = np.linspace(-5, 5, 100)

X, Y = np.meshgrid(x, y)

Z = np.heaviside(np.sin(X) * np.cos(Y), 0.5)

fig = plt.figure()

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

ax.plot_surface(X, Y, Z, cmap='viridis')

ax.set_title("Step Function Grid")

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Importing Required Libraries

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

matplotlib.pyplot is used for plotting.

mpl_toolkits.mplot3d.Axes3D enables 3D plotting support.

numpy is used for numerical operations, especially arrays and math functions.

2. Creating the Grid

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

y = np.linspace(-5, 5, 100)

X, Y = np.meshgrid(x, y)

np.linspace(-5, 5, 100) creates 100 points between -5 and 5 for both x and y.

np.meshgrid(x, y) creates 2D coordinate matrices from the 1D x and y arrays. X and Y are 2D arrays representing all combinations of x and y.

3. Defining the Step Function Surface

Z = np.heaviside(np.sin(X) * np.cos(Y), 0.5)

This defines the Z values (heights) of the surface.

np.sin(X) * np.cos(Y) creates a pattern of values based on sine and cosine waves.

np.heaviside(..., 0.5) converts those values into step-like (binary) outputs:

Returns 1 where the argument is positive,

0 where it's negative,

0.5 exactly at zero (by definition here).

This creates a checkerboard-like grid of 0s and 1s.

 4. Creating the 3D Figure and Axes

fig = plt.figure()

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

plt.figure() initializes a new figure.

add_subplot(111, projection='3d') adds one 3D subplot to the figure.

 5. Plotting the Surface

ax.plot_surface(X, Y, Z, cmap='viridis')

plot_surface() creates a 3D surface plot.

X, Y, Z define the surface coordinates.

cmap='viridis' sets the color gradient for the surface.

 6. Adding Title and Displaying the Plot

ax.set_title("Step Function Grid")

plt.show()

set_title() adds a title to the plot.

plt.show() renders and displays the plot window.


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)