import numpy as np
import matplotlib.pyplot as plt
size = 300
x = np.linspace(-5, 5, size)
y = np.linspace(-5, 5, size)
X, Y = np.meshgrid(x, y)
elevation = np.exp(-((X)**2 + (Y)**2))
distance = np.sqrt(X**2 + Y**2)
lava_flow = np.exp(-distance * 2) * elevation
terrain_noise = 0.05 * np.random.rand(*lava_flow.shape)
lava_flow += terrain_noise
plt.figure(figsize=(6, 6))
plt.imshow(lava_flow, cmap='hot', extent=[-5, 5, -5, 5], origin='lower')
plt.colorbar(label='Lava Flow Intensity')
plt.title("Lava Flow Map from Volcano", fontsize=16)
plt.xlabel("Distance East")
plt.ylabel("Distance North")
plt.grid(False)
plt.tight_layout()
plt.show()
#source code --> clcoding.com
Code Explanation:
Step 1: Import Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy (np): A powerful library for numerical
computations. We use it here to create arrays and perform mathematical
operations.
size = 300
x = np.linspace(-5, 5, size)
y = np.linspace(-5, 5, size)
X, Y = np.meshgrid(x, y)
size = 300: We define the size of the grid. This
will create a 300x300 grid of values (i.e., 300 points in both the x and y
directions).
elevation = np.exp(-((X)**2 + (Y)**2))
This line creates a volcano peak at the center of
the grid (at the point (0, 0)).
distance = np.sqrt(X**2 + Y**2)
lava_flow = np.exp(-distance * 2) * elevation
distance = np.sqrt(X**2 + Y**2): This calculates the
distance from each point on the grid to the center (0,0), which is where the
volcano is located. This helps determine how far each point is from the
volcano's peak.
lava_flow = np.exp(-distance * 2) * elevation: This
line combines the lava flow decay (np.exp(-distance * 2)) with the volcanic
peak (elevation). The lava is more intense around the peak and fades as it
spreads outward, creating a more realistic lava flow pattern.
terrain_noise = 0.05 *
np.random.rand(*lava_flow.shape)
lava_flow += terrain_noise
np.random.rand(*lava_flow.shape): This generates a
random noise array with the same shape as lava_flow. The values are between 0
and 1.
plt.figure(figsize=(8, 6))
plt.imshow(lava_flow, cmap='hot', extent=[-5, 5, -5,
5], origin='lower')
plt.colorbar(label='Lava Flow Intensity')
plt.title("Lava Flow Map from Volcano",
fontsize=16)
plt.xlabel("Distance East")
plt.ylabel("Distance North")
plt.grid(False)
plt.tight_layout()
plt.show()
plt.figure(figsize=(8, 6)): Creates a new figure
with a size of 8x6 inches.
plt.show(): Displays the plot.


0 Comments:
Post a Comment