How to arrange the correct range in colorbar in MATLAB
1 view (last 30 days)
Show older comments
Hello,
I have a 3D surface curvefit in matlab. The issue is for Z column the range is outside of the data range. How can I give correct range? This are my data and this is my code and actually it gives me wrong plot maybe. Would you pelase check it? Also, I want to remove the data points from the plot, how to do that?
Or if you have any other way to plot I would ppreciate it
clear all
M=readtable('tt.xlsx');
x1 = M.(1) ;
y1 = M.(2) ;
z1 = M.(3);
s = fit([x1 y1],z1,"poly23");
plot(s,[x1 y1],z1);
c = jet(20);
colormap(c);
shading interp
xlabel('HR')
ylabel('L/D')
zlabel('Fc')
colorbar
0 Comments
Accepted Answer
Mathieu NOE
on 7 Apr 2022
hello
as I don't have the toolbox for fit, I used instead the FEX submission (excellent) from John D'Errico :
you can simply comment the line with scatter3 plot if you don't want the points on the plot
no issue so far...
code :
clear all
M=readtable('tt.xlsx');
x1 = M.(1) ;
y1 = M.(2) ;
z1 = M.(3);
s = polyfitn([x1(:) y1(:)], z1(:), 2); % File Exchange by John D'Errico : https://fr.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
[xx,yy] = meshgrid(0:1:10, 0:10:400);
zz = polyvaln(s, [xx(:) yy(:)]);
zz = reshape(zz, size(xx));
% remove data outise range
ind = (zz>=max(z1));
zz(ind) = NaN;
% plot points and surface
figure('Renderer','opengl')
scatter3(x1, y1, z1,'o','filled'); % comment this line if you don't want the points
surface(xx, yy, zz, ...
'FaceColor','interp', 'EdgeColor','b', 'FaceAlpha',0.4)
AZ = -37.5;
EL = 30;
view(AZ,EL)
grid on;
colormap(jet);
shading interp
xlabel('HR')
ylabel('L/D')
zlabel('Fc')
colorbar;
5 Comments
Mathieu NOE
on 8 Apr 2022
hello again
first , a small improvement of the code
- mesh is shown (can be removed by commenting this line :
mesh(xx, yy, zz, 'FaceAlpha',0.4)
- meshgrid limits are computed automatically from min and max values of x1 and y1 (I took 25 steps for both directions, you can of course change that)
[xx,yy] = meshgrid(linspace(min(x1),max(x1),25), linspace(min(y1),max(y1),25));
full code :
clear all
M=readtable('tt.xlsx');
x1 = M.(1) ;
y1 = M.(2) ;
z1 = M.(3);
s = polyfitn([x1(:) y1(:)], z1(:), 2); % File Exchange by John D'Errico : https://fr.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
[xx,yy] = meshgrid(linspace(min(x1),max(x1),25), linspace(min(y1),max(y1),25));
zz = polyvaln(s, [xx(:) yy(:)]);
zz = reshape(zz, size(xx));
% remove data outside range
ind = (zz>=max(z1));
zz(ind) = NaN;
% plot points and surface
figure('Renderer','opengl')
hold on
scatter3(x1, y1, z1,'o','filled'); % comment this line if you don't want the points
mesh(xx, yy, zz, 'FaceAlpha',0.4)
surface(xx, yy, zz,'FaceColor','interp', 'EdgeColor','b', 'FaceAlpha',0.4)
hold off
AZ = -37.5;
EL = 30;
view(AZ,EL)
grid on;
colormap(jet);
shading interp
xlabel('HR')
ylabel('L/D')
zlabel('Fc')
colorbar;
%% NOTA : difference between surf and surface matlab functions
% The difference is that surf checks first to see if the axes Nextplot property says to replace plot,
% and if so then it does a cla. Then either way it calls surface()
% In other words if "hold on" is not in effect (or the internal equivalent)
% then surf will erase the current plot. surface() will not do that.
% The difference is sort of like the difference between line() and plot(). line() never erases the current plot.
% plot() might erase the current plot, and whether it does or not it will call line to do the drawing work.
Mathieu NOE
on 8 Apr 2022
FYI the code works too (at least on my side) with the new data file (mmm.xlsx)
surface of order 1 :
s = polyfitn([x1(:) y1(:)], z1(:), 1); %
surface of order 2 :
s = polyfitn([x1(:) y1(:)], z1(:), 2); %
More Answers (0)
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!