Sunday, 18 May 2025

Lava Flow Map from Volcano Pattern using Python

 



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.

 matplotlib.pyplot (plt): A plotting library for visualizing data. We will use it to create the heatmap of the lava flow.

 Step 2: Define Grid for Terrain

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).

 np.linspace(-5, 5, size): This creates a 1D array of size points that are evenly spaced between -5 and 5 for both x and y axes. This represents the geographic area of the map.

 np.meshgrid(x, y): This converts the 1D arrays x and y into a 2D grid (X, Y). This grid represents the terrain coordinates, where each point corresponds to a position in the x-y plane.

 Step 3: Simulate the Volcano Peak (Elevation)

elevation = np.exp(-((X)**2 + (Y)**2))

This line creates a volcano peak at the center of the grid (at the point (0, 0)).

 The function np.exp(-((X)**2 + (Y)**2)) is a Gaussian function, which produces a bell-shaped curve. The center of this curve is at (0, 0), where the volcano's peak is located.

 At the center, the value is 1 (the highest point).

 As you move away from the center, the values decrease exponentially, simulating the decreasing elevation as you move away from the volcano.

 Step 4: Simulate Lava Flow

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.

 np.exp(-distance * 2): This part simulates the lava flow. As lava moves farther from the volcano, it loses intensity (cools down). The exponential decay (np.exp(-distance * 2)) represents this cooling effect. The value is higher near the volcano and gradually decreases as the distance from the volcano increases.

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.

 Step 5: Add Terrain Noise (Optional)

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.

 0.05 * np.random.rand(*lava_flow.shape): The noise is scaled by 0.05 to ensure the noise isn't too overwhelming. This makes the lava flow look more irregular and natural.

 lava_flow += terrain_noise: The noise is added to the lava_flow array, creating small variations in the lava intensity across the grid.

 Step 6: Plot the Lava Flow Map

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.imshow(lava_flow, cmap='hot', extent=[-5, 5, -5, 5], origin='lower'):

 lava_flow: This is the data (lava flow map) that we want to visualize.

 cmap='hot': Specifies the color map for the heatmap. The 'hot' color map uses red, orange, and yellow to represent high values (hot lava) and black for low values (cool lava).

 extent=[-5, 5, -5, 5]: This defines the axis limits. The plot will show the terrain from -5 to 5 in both the x and y directions.

 origin='lower': This sets the origin of the plot to be in the lower-left corner (this is common for maps and geographical data).

 plt.colorbar(label='Lava Flow Intensity'): Adds a color bar to the right of the plot, showing the lava flow intensity values. The label explains what the color bar represents.

 plt.title("Lava Flow Map from Volcano", fontsize=16): Adds a title to the plot.

 plt.xlabel("Distance East"), plt.ylabel("Distance North"): Labels the x and y axes to represent East-West and North-South directions, respectively.

 plt.grid(False): Disables the grid lines on the plot for a cleaner image.

 plt.tight_layout(): Ensures that the layout is adjusted so that the elements (like labels) are properly spaced and not cut off.

plt.show(): Displays the plot.

 


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)