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