how to create a 3D height map
Asked Answered
A

2

7

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.

Aurelia answered 8/6, 2015 at 10:36 Comment(6)
Simpler than plot(xs, ys, zs) ?? Sounds hard, what is your problem exactly?Rech
why is meshgrid difficult ?Bullwhip
meshgrid is not difficult. I'm just wondering if this is already the easiest way to do it.Aurelia
And one more thing is that if I use the method in the link I provided, then basically it very inconvenient for me to rotate the graph and see the details of the graph because there are so many points (Z is something like 500x500 array) and the pop-out window of the graph just keeps lagging. Are there other plots that can alleviate this problem and provide a similar result?Aurelia
Do you need it as surface? Often the same information is contained in a simple 2d plot like imshow or matshow. And they are much faster for big matrices.Thearchy
I've tried imshow, but I feel that a 3D surface is the best display option for the work I'm doingAurelia
F
8

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

Forsooth answered 8/6, 2015 at 10:54 Comment(0)
S
10

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

Set answered 11/7, 2018 at 14:28 Comment(1)
ax.invert_yaxis() if you want the plot origin of x=0, y=0Allspice
F
8

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

Forsooth answered 8/6, 2015 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.