# Problem with Plot 3D

2 views (last 30 days)
ragnor on 19 Jul 2021
Commented: ragnor on 23 Jul 2021
Hello Matlab Community,
I am trying to plot a 3D coloured with some data, however the plot is looking a bit different. The negative and positive values in the Z matrix are identical only difference is the sign. But the plot doesnt look identical in the negative and positive axis. It would be great if someone could help me with this.
x = data(:,1:3);
x = table2array(x);
t = data(:,4);
t = table2array(t);
X = x(:,1);
Y = x(:,3);
Z = t;
N = 200 ;
x = linspace(min(X),max(X),N) ;
y = linspace(min(Y),max(Y),N) ;
[Xi,Yi] = meshgrid(x,y) ;
Zi = griddata(X,Y,Z,Xi,Yi) ;
surf(Xi,Yi,Zi)
Rik on 23 Jul 2021
Why did you delete the excel file? If you don't want public help, you should have hired a private consultant.
Why should the figure (or the axis labels) be removed? Without the rest of the paper, this doesn't say much.
ragnor on 23 Jul 2021
Hello Mr. Rik
I have added the excel sheet back, it was just a silly recording mistake and the corrected file is already uploaded by the person who answered the question.
Also, thanks for you valuable advice. Will keep it mind.
Regards

Cris LaPierre on 19 Jul 2021
Edited: Cris LaPierre on 19 Jul 2021
It looks like, in your Excel sheet, you have erroneously recorded all Y values of -7 as 7. That could explain part of the issue.
Once that is done, you might quickly view the results with the following code.
data.Properties.VariableNames = ["X","F_sw","Y","Z"];
% order the data
data = sortrows(data,["X","Y"]);
X = unique(data.X);
Y = unique(data.Y);
Z = reshape(data.Z,[length(Y),length(X)]);
% View
figure
surf(X,Y,Z) ##### 3 CommentsShow 1 older commentHide 1 older comment
ragnor on 20 Jul 2021
Edited: ragnor on 21 Jul 2021
Hello Cris,
For the same data, i have plotted the 3D surface as suggested by you. I wanted to add legends to the plot. For example, i want to name the red circle markers as something and the interpolated grey area as something. Do you think it is possible?
That would be really helpful if you could help me with this.
data.Properties.VariableNames = ["X","F_sw","Y","Z"];
% order the data
data = sortrows(data,["X","Y"]);
X = unique(data.X);
Y = unique(data.Y);
Z = reshape(data.Z,[length(Y),length(X)]);
% View
figure(1)
s = surf(X,Y,Z);
set(get(gca,'XLabel'),'FontSize',10, 'fontweight', 'normal');
set(get(gca,'YLabel'),'FontSize',10, 'fontweight', 'normal');
set(get(gca,'zLabel'),'FontSize',10, 'fontweight', 'normal');
set(get(gca,'title'),'FontSize',10, 'fontweight', 'normal');
set(s,'Marker','o');
set(s,'MarkerEdgeColor',[0 0 0]);
set(s,'MarkerFaceColor',[1 0 0]);
set(s,'MarkerSize',4);
%set(s,'FaceColor','auto' );
set(s,'FaceColor',[0.784 0.816 0.831] );
legend()
h=figure(1);
ifig1=1;
fileName1='figure';
iChar = num2str(ifig1);
fileName2 = strcat(fileName1,iChar);
fileNameWithExt = strcat('c:\surfplot\',fileName2,'.fig');
saveas(h,fileNameWithExt);
Cris LaPierre on 20 Jul 2021
Legend items correspond to plot objects. If you want two items in your legend, you need two distinct objects in your axes. You currently have one, a surface. Create your surface using surf, and add a second object for your markers, perhaps using scatter, and then you can create a legend with 2 items in it.
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
data.Properties.VariableNames = ["X","F_sw","Y","Z"];
% order the data
data = sortrows(data,["X","Y"]);
X = unique(data.X);
Y = unique(data.Y);
Z = reshape(data.Z,[length(Y),length(X)]);
% View
figure(1)
scatter3(data.X,data.Y,data.Z,12,[1 0 0],"filled","MarkerEdgeColor",[0 0 0])
hold on
s = surf(X,Y,Z,"FaceColor",[0.784 0.816 0.831]);
hold off
ylabel('Phase Current (A)',"FontSize",10,"FontWeight","normal");
zlabel('Error Voltage (V)',"FontSize",10,"FontWeight","normal");
title('title','FontSize',10,'fontweight', 'normal');
legend("Label 1","Label 2") ### Categories

Find more on Labels and Annotations in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!