Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a 3D height map in python

I have a 2D array Z that stores the height at that element's position. Other than using the method here in which I need to create array X and Y with the same size as Z, are there any simpler methods to create a 3D height map?

The 3D surface height map is something like the first surface plot here.

like image 285
Physicist Avatar asked Jun 08 '15 10:06

Physicist


People also ask

Can we plot 3D plot in Python?

We can create 3D plots in Python thanks to the mplot3d toolkit in the matplotlib library. Matplotlib was introduced with only 2D plots in mind. However, as of the 1.0 release, 3D utilities were developed on top of 2D, so 3D implementations of data are available today.

What is 3D in Python?

Python is also capable of creating 3d charts. It involves adding a subplot to an existing two-dimensional plot and assigning the projection parameter as 3d.


2 Answers

Even if I agree with the others that meshgrids are not difficult, still I think that a solution is provided by the Mayavi package (check the function surf)

from mayavi import mlab mlab.surf(Z) mlab.show()

like image 167
Francesco Turci Avatar answered Sep 21 '22 10:09

Francesco Turci


Here the code for matplotlib

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

z = np.array([[x**2 + y**2 for x in range(20)] for y in range(20)])
x, y = np.meshgrid(range(z.shape[0]), range(z.shape[1]))

# show hight map in 3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z)
plt.title('z as 3d height map')
plt.show()

# show hight map in 2d
plt.figure()
plt.title('z as 2d heat map')
p = plt.imshow(z)
plt.colorbar(p)
plt.show()

here the 3D plot of z: enter image description here

and here the 2D plot of z: enter image description here

like image 43
Oliver Wilken Avatar answered Sep 18 '22 10:09

Oliver Wilken