2d surface or contour plot of three independent variables
13 views (last 30 days)
Show older comments
Özgür Alaydin
on 13 Oct 2022
Commented: Star Strider
on 24 Feb 2023
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)
0 Comments
Accepted Answer
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')
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
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
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)
.
More Answers (0)
See Also
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!