import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x=np.linspace(-5,5,30)
y=np.linspace(-5,5,30)
z=np.linspace(-5,5,30)
X,Y,Z=np.meshgrid(x,y,z)
wave=np.sin(2*np.pi*X*10)*np.cos(2*np.pi*Y/10)*np.sin(2*np.pi*Z/10)
threshold=0.5
mask=np.abs(wave)>threshold
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
sc=ax.scatter(X[mask],Y[mask],Z[mask],c=wave[mask],cmap='plasma',s=15,alpha=0.8)
ax.set_title('3D Plasma Wave Simulation')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_box_aspect([1,1,1])
fig.colorbar(sc,shrink=0.6,label='Wave Amplitude')
plt.tight_layout()
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: For numerical operations and generating arrays.
x = np.linspace(-5, 5, 30)
y = np.linspace(-5, 5, 30)
z = np.linspace(-5, 5, 30)
Creates evenly spaced values from -5 to 5 along each
axis (30 points).
X, Y, Z = np.meshgrid(x, y, z)
np.meshgrid converts the 1D arrays into 3D
coordinate grids.
wave = np.sin(2 * np.pi * X / 10) * np.cos(2 * np.pi
* Y / 10) * np.sin(2 * np.pi * Z / 10)
A mathematical expression to simulate a 3D plasma
wave.
threshold = 0.5
mask = np.abs(wave) > threshold
Sets a cutoff (threshold) to visualize only strong
wave amplitudes.
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
Initializes a figure with a 3D subplot.
sc = ax.scatter(X[mask], Y[mask], Z[mask],
c=wave[mask], cmap='plasma', s=15, alpha=0.8)
Plots only the points that passed the threshold
mask.
c=wave[mask]: Colors each point based on wave amplitude.
cmap='plasma': Uses a vibrant colormap.
ax.set_title('3D Plasma Wave Simulation')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_box_aspect([1, 1, 1])
Adds title and axis labels.
fig.colorbar(sc, shrink=0.6, label='Wave Amplitude')
plt.tight_layout()
plt.show()
Adds a color bar indicating amplitude values.


0 Comments:
Post a Comment