import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
heat_source_x, heat_source_y = 0, 0
sigma = 1.5
Z = np.exp(-((X - heat_source_x)**2 + (Y - heat_source_y)**2) / (2 * sigma**2))
fig=plt.figure(figsize=(10,7))
ax=fig.add_subplot(111,projection="3d")
ax.plot_surface(X,Y,Z,cmap="hot",edgecolor="none")
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Heat Intensity")
ax.set_title("Heat Wave Mirage pattern")
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
plt.show()
#source code --> clcoding.com
Code Explanation:
Import
Required Libraries
import numpy as np
import
matplotlib.pyplot as plt
from
mpl_toolkits.mplot3d import Axes3D
NumPy (np): Used
for creating numerical arrays and mesh grids.
Matplotlib (plt): Used for creating the 3D surface plot.
mpl_toolkits.mplot3d: Enables 3D plotting in Matplotlib.
Create
a Grid for Heat Simulation
x = np.linspace(-5,
5, 100) # X-axis range (-5 to 5) with
100 points
y = np.linspace(-5,
5, 100) # Y-axis range (-5 to 5) with
100 points
X, Y =
np.meshgrid(x, y) # Create a 2D mesh
grid from x and y values
np.linspace(-5, 5,
100): Creates 100 evenly spaced points from -5 to 5 in both x and y directions.
np.meshgrid(x, y): Creates a grid of (X, Y) points where each (x, y) pair forms a coordinate in a 2D plane.
Define
Heat Source and Heat Diffusion Formula
heat_source_x,
heat_source_y = 0, 0 # Heat source at
(0,0)
sigma = 1.5 # Controls the spread of heat
Z = np.exp(-((X -
heat_source_x)**2 + (Y - heat_source_y)**2) / (2 * sigma**2))
(heat_source_x,
heat_source_y): Defines the center of the heat source.
sigma: Controls how
fast heat dissipates (higher value = wider spread).
This equation
models how heat intensity decreases as we move away from the heat source.
High intensity at
(0,0) and gradual fading outward.
Create
a 3D Plot
fig =
plt.figure(figsize=(10, 7)) # Create
figure with size 10x7
ax =
fig.add_subplot(111, projection="3d")
# Add 3D subplot
fig =
plt.figure(figsize=(10, 7)): Initializes a Matplotlib figure.
ax = fig.add_subplot(111, projection="3d"): Creates a 3D plot area (ax).
Plot
Heat Distribution as a 3D Surface
ax.plot_surface(X,
Y, Z, cmap="hot", edgecolor="none")
plot_surface(X, Y,
Z): Creates a 3D surface plot.
cmap="hot":
Uses the "hot" color map, where:
Red & yellow =
High heat
Black & dark
orange = Low heat
edgecolor="none":
Removes edges to make the plot smooth.
Customize
Labels and Titles
ax.set_xlabel("X
Axis")
ax.set_ylabel("Y
Axis")
ax.set_zlabel("Heat
Intensity")
ax.set_title("Heatwave
Mirage - 3D Heat Diffusion Simulation")
Adds labels for X,
Y, and Z axes.
Sets the plot
title.
Remove Grid Ticks for a Clean Look
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
Hides grid ticks
for a smoother visualization.
Display
the Final Heatwave Mirage
plt.show()
Displays the 3D
plot.
0 Comments:
Post a Comment