How to connect 3d plot lines with a surface?
3 views (last 30 days)
Show older comments
I have matlab code that will plot a 3d line for each group of data points that I specify. Now I want to create a surface that connects all of the plot lines. Like a trend line for data in excel, but in this case I need it to be a surface not a line. How can I do that?
Initially I tried using surf, but the plot it returned was very choppy and not a smooth surface. However that was before I split the data into groups. (plot3 used to give me a zigzag line before I split the data into groups). I am currently using a loop to group my data and plot each line, but I'm struggling with applying that to creating a surface (you can see in the code below that I tried a surf within the loop but commented it out).

if true
data = xlsread('NeedleTest.xls') ;
x=data(:,1);
p=[0.05 0.1 0.03 0.07 0.08 0.09 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2];
hold on
grid on
count=1;
while count<numel(p);
m=find(x==p(count));
data2=data(m,1:3);
%s=scatteredInterpolant(data2(:,1), data2(:,2), data2(:,3))
%[X Y]=meshgrid(data2(:,1), data2(:,2))
%s=surf(X,Y,data2)
plot3(data2(:,1), data2(:,2), data2(:,3))
count=count+1;
end
xlabel('Percent Open')
ylabel('Pressure H2O')
zlabel('Flow lpm')
0 Comments
Answers (1)
Jacob Mathew
on 6 Dec 2024
Hi Julia,
You can utilise the meshgrid function to create the surface that you want. Here is an example:
% Sample data for three lines
x = 1:10;
y1 = rand(1, 10);
y2 = rand(1, 10) + 1;
y3 = rand(1, 10) + 2;
z1 = linspace(0, 1, 10);
z2 = linspace(1, 2, 10);
z3 = linspace(2, 3, 10);
% Plotting the lines by itself
figure;
plot3(x, y1, z1, '-o', 'DisplayName', 'Line 1', 'LineWidth', 2);
hold on;
plot3(x, y2, z2, '-x', 'DisplayName', 'Line 2', 'LineWidth', 2);
plot3(x, y3, z3, '-s', 'DisplayName', 'Line 3', 'LineWidth', 2);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Lines');
legend;
grid on;
view(3);
% Combine y and z data into matrices for surface plotting
Y = [y1; y2; y3];
Z = [z1; z2; z3];
% Create a meshgrid for x values
[X, Zgrid] = meshgrid(x, [0 1 2]);
% Plot the surface and the lines
figure;
surf(X, Y, Z, 'FaceAlpha', 0.5); % transparency for better visibility of lines
hold on;
plot3(x, y1, z1, '-o', 'DisplayName', 'Line 1', 'LineWidth', 2);
plot3(x, y2, z2, '-x', 'DisplayName', 'Line 2', 'LineWidth', 2);
plot3(x, y3, z3, '-s', 'DisplayName', 'Line 3', 'LineWidth', 2);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Surface Connecting Lines');
legend;
grid on;
view(3);
You can reference the documentation for meshgrid in the link below:
0 Comments
See Also
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!
