-
Notifications
You must be signed in to change notification settings - Fork 0
/
topographic-surface.py
61 lines (54 loc) · 2.17 KB
/
topographic-surface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
# -----------------------------------------------------------------------------
#
# Load data from CSV file
#
# -----------------------------------------------------------------------------
my_data = np.genfromtxt('datasets/baby-twins.csv', delimiter=',')
# -----------------------------------------------------------------------------
#
# Handle missing values in the data
#
# -----------------------------------------------------------------------------
my_data[my_data == 0] = np.nan
my_data = my_data[~np.isnan(my_data).any(axis=1)]
# -----------------------------------------------------------------------------
#
# Extract the coordinates and define grid for interpolation
#
# -----------------------------------------------------------------------------
X = my_data[:, 0]
Y = my_data[:, 1]
Z = my_data[:, 2]
xi = np.linspace(X.min(), X.max(), 100)
yi = np.linspace(Y.min(), Y.max(), 100)
# -----------------------------------------------------------------------------
#
# Interpolate data onto grid using linear method with griddata
#
# -----------------------------------------------------------------------------
zi = griddata((X, Y), Z, (xi[None, :], yi[:, None]), method='linear')
# -----------------------------------------------------------------------------
#
# Create a meshgrid for plotting the 3D surface
#
# -----------------------------------------------------------------------------
xig, yig = np.meshgrid(xi, yi)
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
surf = ax.plot_surface(xig, yig, zi, cmap='gist_earth', edgecolor='none')
# -----------------------------------------------------------------------------
#
# Add color bar and labels, and set the Z-axis limits based on data range
#
# -----------------------------------------------------------------------------
cbar = fig.colorbar(surf, shrink=0.7, aspect=20, pad=0.2)
cbar.set_label('Elevation (m)')
ax.set_title('3D Surface Plot')
ax.set_xlabel('Latitude', labelpad=15)
ax.set_ylabel('Longitude', labelpad=15)
ax.set_zlabel('Altitude', labelpad=20)
ax.set_zlim(Z.min(), Z.max())
plt.show()