Having trouble plotting 3D data

4 views (last 30 days)
Tawheed Uddin
Tawheed Uddin on 2 Dec 2021
Answered: Simran on 24 Feb 2025
hi i am having trouble plotting this 3d graph. this is the data i was using and the code
X=[0.50 2.70 1.80 1.45 1.35 1.13 0.96 1.49 1.72 0.80 1.03 1.19 0.75 1.03 1.20 1.31 1.52 2.15 2.22 2.30 2.42 2.50 2.62 2.68 1.79 1.97 1.78]';
Y=[0.240 3.500 1.950 1.800 1.600 1.300 0.900 2.170 2.330 0.980 1.170 1.370 1.120 1.330 1.500 1.640 1.800 0.850 1.0 0.8 0.7 0.9 0.9 0.8 0.8 1.1 1.1]';
Z=[342.8880559 1361.877793 1012.830906 923.1590142 871.7488789 774.2139069 656.8934189 981.1561986 1050.146934 636.0585104 728.1907481 799.5898076 645.9988011 755.886652 823.2101723 869.3645606 937.4594935 794.5434106 853.1103438 778.2601977 727.3768847 839.8072314 859.4947972 823.6481171 744.4141472 862.0806767 839.9441428]';
Z=[X Y Z];
surf(Z);shading('interp');
xlabel('X');ylabel('Y');zlabel('Z');
However, this is the graph i get
i do not know what i have doen wrong. any help would be greatly appreciated.
thanks.

Answers (1)

Simran
Simran on 24 Feb 2025
It is my understanding that you are trying to plot a 3D curve for your data but the issue with your code is that you're trying to use ”surf with a 1D array which is not suitable for this function. The ”surf” function requires a grid of points, not just a list of coordinates.
To solve this and plot your data in 3D, you can use the scatter3 function instead. Here’s how to do it:
X = [0.50 2.70 1.80 1.45 1.35 1.13 0.96 1.49 1.72 0.80 1.03 1.19 0.75 1.03 1.20 1.31 1.52 2.15 2.22 2.30 2.42 2.50 2.62 2.68 1.79 1.97 1.78]';
Y = [0.240 3.500 1.950 1.800 1.600 1.300 0.900 2.170 2.330 0.980 1.170 1.370 1.120 1.330 1.500 1.640 1.800 0.850 1.0 0.8 0.7 0.9 0.9 0.8 0.8 1.1 1.1]';
Z = [342.8880559 1361.877793 1012.830906 923.1590142 871.7488789 774.2139069 656.8934189 981.1561986 1050.146934 636.0585104 728.1907481 799.5898076 645.9988011 755.886652 823.2101723 869.3645606 937.4594935 794.5434106 853.1103438 778.2601977 727.3768847 839.8072314 859.4947972 823.6481171 744.4141472 862.0806767 839.9441428]';
scatter3(X, Y, Z, 'filled');
xlabel('X');
ylabel('Y');
zlabel('Z');
If you want to create surface plot only, you need to have a grid of X and Y values, and corresponding Z values for each (X, Y) pair. Since your data is given as individual points, you'll need to interpolate these points onto a grid. Here’s how you can do that:
% Original data
X = [0.50 2.70 1.80 1.45 1.35 1.13 0.96 1.49 1.72 0.80 1.03 1.19 0.75 1.03 1.20 1.31 1.52 2.15 2.22 2.30 2.42 2.50 2.62 2.68 1.79 1.97 1.78]';
Y = [0.240 3.500 1.950 1.800 1.600 1.300 0.900 2.170 2.330 0.980 1.170 1.370 1.120 1.330 1.500 1.640 1.800 0.850 1.0 0.8 0.7 0.9 0.9 0.8 0.8 1.1 1.1]';
Z = [342.8880559 1361.877793 1012.830906 923.1590142 871.7488789 774.2139069 656.8934189 981.1561986 1050.146934 636.0585104 728.1907481 799.5898076 645.9988011 755.886652 823.2101723 869.3645606 937.4594935 794.5434106 853.1103438 778.2601977 727.3768847 839.8072314 859.4947972 823.6481171 744.4141472 862.0806767 839.9441428]';
% Define the grid
xlin = linspace(min(X), max(X), 100); % Adjust the number of points as needed
ylin = linspace(min(Y), max(Y), 100);
[Xgrid, Ygrid] = meshgrid(xlin, ylin);
% Interpolate Z values onto the grid
Zgrid = griddata(X, Y, Z, Xgrid, Ygrid, 'cubic'); % Use 'cubic' or 'linear' interpolation
% Plot the surface
surf(Xgrid, Ygrid, Zgrid);
shading interp;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Surface Plot');
Here’s the attached documentation that you may find relevant:

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!