How do I create a 3 dimensional surface from X Y Z points
Show older comments
Hi all,
I am struggling a bit here, and hope somebody could help. I have a set of points from a complex function that I am trying to produce a 3D shape of, and have had no luck so far. I used Python to find the points in a .txt format. From there, I copy the data to Excel to transpose the columns into rows for Matlab use. The problem is, I can't seem to figure out a code to just take the X, Y, and Z coordinates and produce some kind of mesh grid or figure. Does anybody have a code for this?
Here is an example of just (10) points from my function. I can tell Python to produce anywhere from 10 to 10,000 points.
x = [422 424 424 422 422 421 421 425 421 424]; y = [87 96 87 87 83 93 85 88 86 91]; z = [92.73 94.88 92.92 92.73 92.04 93.92 92.24 93.23 92.42 93.78];
Best regards,
Eric.
Answers (3)
Star Strider
on 9 Mar 2018
x = [422 424 424 422 422 421 421 425 421 424];
y = [87 96 87 87 83 93 85 88 86 91];
z = [92.73 94.88 92.92 92.73 92.04 93.92 92.24 93.23 92.42 93.78];
figure(1)
stem3(x, y, z)
grid on
xv = linspace(min(x), max(x), 20);
yv = linspace(min(y), max(y), 20);
[X,Y] = meshgrid(xv, yv);
Z = griddata(x,y,z,X,Y);
figure(2)
surf(X, Y, Z);
grid on
set(gca, 'ZLim',[0 100])
shading interp
Experiment to get the result you want.
6 Comments
Stephen23
on 9 Mar 2018
Thanks for the help, is there any way I can use the surf function or produce some kind of grid that shows the contours of my points?
Star Strider
on 9 Mar 2018
My pleasure.
The griddata function will interpolate a varying surface if the variation exists. It produced a plane here because the plane defined the points provided. (See the documentation on griddata for details.)
I am not certain what you want. The surf function depicts a surface. The surfc function will provide an additional contour below the surface. The contour3 function will (with the hold function) draw contours on the surface. The contour and contourf functions will draw a contour plot.
@Stephen — Thank you.
Sophia Dauenhauer
on 25 Jan 2022
Thank you so much... this helped me figure out plotting a 3d surface from given x y z data points.
Star Strider
on 26 Jan 2022
Felipe Alvarez Arroyo
on 19 May 2022
thankyou
Star Strider
on 19 May 2022
@Felipe Alvarez Arroyo — My pleasure!
Votes are always appreciated!
x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

Al Danial
on 20 May 2022
Since you're starting in Python you can make the surface plot there too if you want. This is a translation of @Star Strider's matlab solution:
#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from scipy.interpolate import griddata
x = np.array([422, 424, 424, 422, 422, 421, 421, 425,
421, 424])
y = np.array([87, 96, 87, 87, 83, 93, 85, 88, 86, 91])
z = np.array([92.73, 94.88, 92.92, 92.73, 92.04, 93.92,
92.24, 93.23, 92.42, 93.78])
xv = np.linspace(np.min(x), np.max(x), 20)
yv = np.linspace(np.min(y), np.max(y), 20)
[X,Y] = np.meshgrid(xv, yv)
Z = griddata((x,y),z,(X,Y),method='linear')
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_xlabel('X')
ax.set_ylabel('Y')
fig.colorbar(surf, shrink=0.6)
plt.show()
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!