Wednesday, 30 April 2025

3D Atmospheric Cloud Simulation using Python

 




import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
np.random.seed(42)
cloud_volume=np.random.rand(30,30,30)
x,y,z=np.indices(cloud_volume.shape)
threshold=0.7
mask=cloud_volume>threshold
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
ax.scatter(x[mask],y[mask],z[mask],c='white',alpha=0.5,s=10)
ax.set_facecolor('skyblue')
ax.set_title('3D Atmospheric Cloud Simulation')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_xlabel('Z axis')
ax.set_box_aspect([1,1,1])
plt.tight_layout()
plt.show()
#source code --> clcoding.com 

Code Explanation:

1. Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: For numerical operations and array manipulations.
matplotlib.pyplot: For creating plots and visualizations.
Axes3D: Enables 3D plotting functionality in Matplotlib.

2. Create a 3D Volume (Simulated Cloud Data)
np.random.seed(42)
cloud_volume = np.random.rand(30, 30, 30)
np.random.seed(42): Sets a fixed seed so the random values are reproducible.

cloud_volume = np.random.rand(30, 30, 30): Generates a 3D array (30×30×30) of random values between 0 and 1, representing cloud density.

3. Create Grid Indices for the Volume
x, y, z = np.indices(cloud_volume.shape)
np.indices(): Creates coordinate grid arrays corresponding to each voxel in the 3D space. Now you have x, y, and z arrays of shape (30, 30, 30) for mapping points.

4. Apply a Density Threshold
threshold = 0.7
mask = cloud_volume > threshold
threshold = 0.7: Defines a cutoff for what’s considered "dense enough" to visualize.

mask = cloud_volume > threshold: Creates a boolean mask where only voxels with density greater than 0.7 are selected as cloud points.

5. Plot the Cloud Points
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
plt.figure(figsize=(10, 8)): Sets up the figure window with a specific size.

projection='3d': Enables 3D plotting inside the subplot.

6. Scatter Plot the Cloud Voxels
ax.scatter(x[mask], y[mask], z[mask], c='white', alpha=0.5, s=10)
ax.scatter(): Plots each voxel as a white semi-transparent dot.

x[mask], y[mask], z[mask]: Only plots the voxels that passed the threshold.

alpha=0.5: Controls transparency (semi-transparent clouds).

s=10: Dot size.

7. Style and Label the Plot
ax.set_facecolor('skyblue')
ax.set_title("3D Atmospheric Cloud Simulation")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
ax.set_box_aspect([1,1,1])
ax.set_facecolor('skyblue'): Gives the background a sky-blue color to resemble the atmosphere.

set_title, set_xlabel, set_ylabel, set_zlabel: Adds plot and axis labels.

set_box_aspect([1,1,1]): Ensures equal scaling across all axes for a proportional 3D view.

8. Finalize and Display the Plot
plt.tight_layout()
plt.show()
plt.tight_layout(): Adjusts layout so nothing overlaps.

plt.show(): 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)