import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter
size=500
terrain=np.zeros((size,size))
def add_crater(map,x0,y0,radius,depth):
x=np.arange(map.shape[0])
y=np.arange(map.shape[1])
X,Y=np.meshgrid(x,y,indexing='ij')
crater = -depth * np.exp(-(((X - x0)**2 + (Y - y0)**2) / (2 * radius**2)))
map+=crater
num_craters=100
np.random.seed(42)
for _ in range(num_craters):
x=np.random.randint(0,size)
y=np.random.randint(0,size)
radius=np.random.randint(5,25)
depth=np.random.uniform(0.1,0.6)
add_crater(terrain,x,y,radius,depth)
terrain=gaussian_filter(terrain,sigma=1)
plt.figure(figsize=(6,6))
plt.imshow(terrain,cmap='gray',origin='lower')
plt.title('Moon Crater Pattern using Python')
plt.axis('off')
plt.colorbar(label='Depth')
plt.tight_layout()
plt.show()
#source code --> clcoding.com
Code Explanation:
Step 1: Import Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
numpy (as np): Used for numerical operations,
creating arrays, and random number generation.
size = 500
terrain = np.zeros((size, size))
size = 500: Defines the grid size of the terrain,
creating a 500x500 map of the surface.
def add_crater(map, x0, y0, radius, depth):
x =
np.arange(map.shape[0])
y =
np.arange(map.shape[1])
X, Y =
np.meshgrid(x, y, indexing='ij')
crater =
-depth * np.exp(-(((X - x0)**2 + (Y - y0)**2) / (2 * radius**2)))
map +=
crater
add_crater() is a function that adds a
Gaussian-shaped crater to the map.
num_craters = 100
np.random.seed(42)
# Reproducibility
x =
np.random.randint(0, size)
y =
np.random.randint(0, size)
radius =
np.random.randint(5, 25)
depth =
np.random.uniform(0.1, 0.6)
add_crater(terrain, x, y, radius, depth)
num_craters = 100: Sets the number of craters to be
added to the map. In this case, 100 random craters will be placed.
terrain = gaussian_filter(terrain, sigma=1)
gaussian_filter(terrain, sigma=1): Smooths the
terrain slightly using a Gaussian filter.
plt.figure(figsize=(8, 8))
plt.imshow(terrain, cmap='gray', origin='lower')
plt.title("Moon Crater Pattern",
fontsize=16)
plt.axis('off')
plt.colorbar(label="Depth")
plt.tight_layout()
plt.show()
plt.figure(figsize=(8, 8)): Creates a new figure for
the plot with a size of 8x8 inches.
plt.imshow(terrain, cmap='gray', origin='lower'):
terrain: The data to be visualized (the cratered
surface).
cmap='gray': Uses a grayscale color map to visualize
the depths. Lighter shades represent higher elevations, while darker shades
represent lower elevations (craters).
origin='lower': Ensures that the origin of the plot
is at the bottom-left, as is standard for most geographic data.
plt.title("Moon Crater Pattern",
fontsize=16): Adds a title to the plot.
plt.axis('off'): Hides the axis lines and labels for
a cleaner visualization.
plt.colorbar(label="Depth"): Adds a color
bar to the side of the plot to show the depth values, so you can interpret the
grayscale values in terms of depth.
plt.tight_layout(): Adjusts the layout to ensure the
plot fits well in the figure without any clipping.
plt.show(): Displays the plot.


.png)
.png)

.png)
.png)
.png)

.png)


.png)


.png)




