Skip to content Skip to sidebar Skip to footer

Create A Surface Plot Of Xyz Altitude Data In Python

I am trying to create a surface plot of a mountain in python, of which I have some xyz data. The end result should look something like that. The file is formatted as follows: 61600

Solution 1:

Using functions plot_trisurf and scatter from matplotlib, given X Y Z data can be plotted similar to given plot.

import sys
import csv
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

# Read CSV
csvFileName = sys.argv[1]
csvData = []
withopen(csvFileName, 'r') as csvFile:
    csvReader = csv.reader(csvFile, delimiter=' ')
    for csvRow in csvReader:
        csvData.append(csvRow)

# Get X, Y, Z
csvData = np.array(csvData)
csvData = csvData.astype(np.float)
X, Y, Z = csvData[:,0], csvData[:,1], csvData[:,2]

# Plot X,Y,Z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5)
ax.scatter(X, Y, Z, c='red')
plt.show()

Here,

  • file containing X Y Z data provided as argument to above script
  • in plot_trisurf, parameters used to control appearance. e.g. alpha used to control opacity of surface
  • in scatter, c parameter specifies color of points plotted on surface

For given data file, following plot is generated

enter image description here

Note: Here, the terrain is formed by triangulation of given set of 3D points. Hence, contours along surface in plot are not aligned to X- and Y- axes

Solution 2:

import numpy as np  
import matplotlib.pyplotas plt  
import mpl_toolkits.mplot3dimport pandas as pd 
 
df = pd.read_csv("/content/1.csv") 
X = df.iloc[:, 0] 
Y = df.iloc[:, 1] 
Z = df.iloc[:, 2] 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
ax.plot_trisurf(X, Y, Z, color='white', edgecolors='grey', alpha=0.5) 
ax.scatter(X, Y, Z, c='red') 
plt.show()

My output image below - I had a lot of data points:enter image description here

Solution 3:

There is an easier way to achieve your goal without using pandas.

import numpy as np 
import matplotlib.pyplot as plt 
import mpl_toolkits.mplot3d

x, y = np.mgrid[-2 : 2 : 20j, -2 : 2 : 20j]
z = 50 * np.sin(x + y)                     # test data
output = plt.subplot(111, projection = '3d')   # 3d projection
output.plot_surface(x, y, z, rstride = 2, cstride = 1, cmap = plt.cm.Blues_r)
output.set_xlabel('x')                         # axis label
output.set_xlabel('y')
output.set_xlabel('z')

plt.show()

plot example

Post a Comment for "Create A Surface Plot Of Xyz Altitude Data In Python"