Clear Filters
Clear Filters

Converting fill3 plot to surf plot

15 views (last 30 days)
Yuan
Yuan on 20 Apr 2023
Answered: Shuba Nandini on 24 Apr 2023
Hi,
I have a fill3 plot created using the following data below:
x = [0,18.21,18.05,0]
y = [0,29.90,29.98,0.86]
z = [0,-2.04,-2.32,0]
The resulting plot is as shown:
How can I create a surf plot instead that has a similar shape to this fill3 plot?
Thank you!
  2 Comments
Walter Roberson
Walter Roberson on 20 Apr 2023
could you explain further why it is important that it be a surface() object rather than a patch() object?
Also is it necessary to convert the patch object after calling fill3, or would it be acceptable to work with the data instead of calling fill3?
Walter Roberson
Walter Roberson on 21 Apr 2023
Will the data always be in that form, or could it be more complicated / longer ?
Will the x and z always return to origin but the y not return to origin? The current figure is not a triangle (as it would be if the y returned to origin), it is an odd flexed surface.

Sign in to comment.

Answers (1)

Shuba Nandini
Shuba Nandini on 24 Apr 2023
Hi Yuan,
It is my understanding that you want to generate a similar shape by using “surf” plot as “fill3” plot does but it is possible that the surface plotted by the surf function appears different from the surface plotted by fill3 because they use different algorithms to generate the surface.
“fill3” and “patch” create a surface by connecting the vertices defined by the x, y, and z arrays using triangular faces, while surf generates a surface by interpolating the data in the z array over a grid of x and y values.
Please refer to the below code which uses patch function to generate the similar shape as fill3 function does:
% Define the x, y, and z data for the fill3 plot
x = [0, 18.21, 18.05, 0];
y = [0, 29.90, 29.98, 0.86];
z = [0, -2.04, -2.32, 0];
% vertices of the surface
vertices = [x', y', z'];
% faces of the surface
faces = [1 2 3 4];
% colors for each vertex of the surface is grey
colors = [0.5 0.5 0.5; 0.5 0.5 0.5; 0.5 0.5 0.5; 0.5 0.5 0.5];
% Plot the surface using the patch function
patch('Faces', faces, 'Vertices', vertices, 'FaceVertexCData', colors);
% Set the view angle and axis labels
view(3);
xlabel('x');
ylabel('y');
zlabel('z');
To know more about the “patch” function, please refer to this link.
To generate similar shape with "surf" plot, you can refer to the below code:
% Define the x, y, and z data for the fill3 plot
x = [0, 18.21, 18.05, 0];
y = [0, 29.90, 29.98, 0.86];
z = [0, -2.04, -2.32, 0];
% Create a grid of points using meshgrid
[X,Y] = meshgrid(linspace(min(x),max(x),100),linspace(min(y),max(y),100));
% Interpolate the z values at the grid points
Z = griddata(x,y,z,X,Y);
% Create the surface plot using surf
surf(X,Y,Z,'EdgeColor','none','FaceColor','black');
% Set the view angle and axis labels
view(3);
xlabel('x');
ylabel('y');
zlabel('z');
Without using “meshgrid” and “griddata” functions, the surface plot would be made up of a series of disconnected triangles connecting the input data points. So, by using “meshgrid” and “griddata”, we can create a more accurate and visually appealing surface plot that accurately represents the underlying function.
To know more about "meshgrid" and "griddata", please refer to the below links:
Hope this helps!

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!