2d surface or contour plot of three independent variables

16 views (last 30 days)
Hello all,
i have a set of data as given above in csv format.
I want to plot column 1, 5 and 6 as given in attached plot. I want to plot as 2d surface or contour.
My code is not working.
Please help.
clear
clc
sweep=readtable('TM_sweep.csv');
x = sweep(:,1);
y = sweep(:,5);
z = sweep(:,6);
[X,Y] = meshgrid(x,y);
Z=diag(z);
surface(X,Y,Z)

Accepted Answer

Star Strider
Star Strider on 13 Oct 2022
Edited: Star Strider on 13 Oct 2022
The data are gridded. It is simply necessary to reshape them to plot them —
sweep = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1154768/TM_sweep.csv', 'VariableNamingRule','preserve')
sweep = 6531×6 table
% DC thcg (nm) period (nm) whcg (nm) lambda0 (µm) Total reflectance (1) ____ _________ ___________ _________ ____________ _____________________ 0.45 700 1400 922 2.7 0.98355 0.45 700 1400 922 2.705 0.98288 0.45 700 1400 922 2.71 0.98205 0.45 700 1400 922 2.715 0.98109 0.45 700 1400 922 2.72 0.97998 0.45 700 1400 922 2.725 0.97875 0.45 700 1400 922 2.73 0.97738 0.45 700 1400 922 2.735 0.9759 0.45 700 1400 922 2.74 0.97432 0.45 700 1400 922 2.745 0.97264 0.45 700 1400 922 2.75 0.97087 0.45 700 1400 922 2.755 0.96904 0.45 700 1400 922 2.76 0.96717 0.45 700 1400 922 2.765 0.96527 0.45 700 1400 922 2.77 0.96336 0.45 700 1400 922 2.775 0.96146
x = sweep{:,1};
y = sweep{:,5};
z = sweep{:,6};
VN = sweep.Properties.VariableNames;
[Ux,ix1] = unique(x); % Unique 'x' Values
dim1 = diff(ix1); % Get Row Dimension For Reshaped Matrix
Ud1 = unique(dim1); % Be Certain This Is Constant
X = reshape(x,Ud1,[]);
Y = reshape(y,Ud1,[]);
Z = reshape(z,Ud1,[]);
figure
surf(X, Y, Z)
grid on
xlabel(VN{1})
ylabel(VN{5})
zlabel(VN{6})
colormap(turbo)
colorbar
shading('interp')
figure
contourf(X, Y, Z, 50)
xlabel(VN{1})
ylabel(VN{5})
colormap(turbo)
hcb = colorbar;
hcb.Label.String = VN{6};
Make appropriate changes to get the desired result.
EDIT — (13 Oct 2022 at 12:28)
Added label to contour colorbar.
.
  4 Comments
Harlan Johnson
Harlan Johnson on 23 Feb 2023
I am trying to plot some cruise CTD data in 3-D, and usueally
I cut-and-pasted this example and got this error message. The script loaded all the variables but could
not reshape the vectors into matrices.
Error in example3D (line 9)
X = reshape(x,Ud1,[]);
I am trying to do a 3D plot of some large CTD data from a cruise and am using your examples to learn how to do this. Any help is appriated. Thanks!
Paul Johnson paulj@uw.edu
Star Strider
Star Strider on 24 Feb 2023
If the ‘CTD’ data are gridded, it may be necesary to find the unique values of a different independent variable, since they may not all have the same structure as these data did. If they are not gridded, then it would be necessary to grid them first. This is relatively straightforward, however it would then require the scatteredInterpolant function for the best result with respect to interpolating them to create the required matrices.
I cannot determine by looking at the .m file (that is a copy of my code here) what the best approach would be.
file = websave('example3D','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1305150/example3D.m');
type(file)
readtable('sweep.csv') x = sweep{:,1}; y = sweep{:,5}; z = sweep{:,6}; VN = sweep.Properties.VariableNames; [Ux,ix1] = unique(x); % Unique 'x' Values dim1 = diff(ix1); % Get Row Dimension For Reshaped Matrix Ud1 = unique(dim1); % Be Certain This Is Constant X = reshape(x,Ud1,[]); Y = reshape(y,Ud1,[]); Z = reshape(z,Ud1,[]); figure surf(X, Y, Z) grid on xlabel(VN{1}) ylabel(VN{5}) zlabel(VN{6}) colormap(turbo) colorbar shading('interp')
.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour 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!