# How would you make a contour plot of a 3 column matrix?

39 Ansichten (letzte 30 Tage)
Louis van Herwijnen am 10 Mai 2018
Kommentiert: Katherina Fiege am 17 Jun. 2019
Hello,
I am currently trying to make a contour plot of distance-depth with pCO2 as the z value (in the excel file there are multiple columns but i only need those three). This takes the form of three columns of equal size, however I looked in the forums and it is apparently not possible. Data from the third column needs to be associated to the others in some way that allows the contour plot. My question is: if not possible, how then would you represent a profile such as this, with distance on x, depth on y and z as the value studied ?? I have thought about a scatter but I can't figure out the parameters to trace for example lines of equal values to make it look better than just scattered points... Any ideas ?? Thank you !!
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Wick am 11 Mai 2018
Bearbeitet: Wick am 11 Mai 2018
Edit: I didn't see the Excel spreadsheet on first pass. The answer section has my code to solve this for you.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

John BG am 11 Mai 2018
Bearbeitet: John BG am 12 Mai 2018
Hi Louis van Herwijnen
1.-
acquiring data from the provided Excel file
clear all;clc;close all
taking only 2 decimals
N=2
L=1/10^N*floor(L*10^N)
.
checking whether grid is uniform
.
[D,ni,na]=unique(L);
ni(1)=1 % correction for 1st row because spreadsheet 1st row contains var names only, not numerical data
nD=[diff(ni); numel(L)-ni(end)+1] % amount of measurements at different depths, on same distance
.
The coordinates to use are D: Distance W: Depth
.
d=A(:,1);
w=A(:,2);
% max min values of coordinates:
min(d)
% =
% 0
max(d)
% =
% 6.887000000000001e+02
min(w)
% =
% 1.400000000000000
max(w)
% =
% 3.675300000000000e+03
.
ok, now we see it's a non-uniform grid
2.-
acquiring measurements
T = A(:,3); % Temperature Celcsius
S = A(:,4); % Salinity [psu]
TA = A(:,4); % TA umol/kgSW
TCO2 = A(:,5); % TCO2 umol/kgSW
TpCO2 = A(:,6); % pCO2 uatm
pH = A(:,7); % pH
O2 = A(:,8); % O2 [umol/kgSW
3.-
Example plot without interpolation, on same distance
k=3
hf(1)=figure(1);plot(w([ni(k):1:ni(k+1)-1])',T([ni(k):1:ni(k+1)-1])')
xlabel('W:depth');ylabel('T(ºC)')
title(['Temperature measurements at distance d = ' num2str(d(ni(k)))]);
grid on
% 000-1
.
non-uniform grid of 2D reference points
.
hf(2)=figure(2);plot(d,w,'ro');grid on;grid minor
xlabel('D:distance');ylabel('W:depth')
% 000-2
.
Temperature interpolation
.
F_Temperature=scatteredInterpolant(d,w,T)
% F=scatteredInterpolant(A(:,1),A(:,2),A(:,3))
Q=4 % interpolation factor
d_uniform_range=linspace(min(d),max(d),Q*numel(nD)-1)
w_uniform_range=linspace(min(w),max(w),Q*numel(nD)-1)
[D,W]=meshgrid(d_uniform_range,w_uniform_range)
T2=F_Temperature(D,W)
hf(3)=figure(3);surf(D,W,T2)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['Temperature - interpolation factor Q = ' num2str(Q)])
% 001
.
4.- Temperature contours
.
hf(4)=figure(4);hT_surfc=surfc(D,W,T2)
campos([5578.878486985600 7013.723961265031 21.490607228584])
xlabel('D:distance');ylabel('W:depth')
title('Temp contour projections')
% 002
.
The projected contours are available in here:
hT_surfc(2)
ans =
Contour with properties:
LineColor: 'flat'
LineStyle: '-'
LineWidth: 0.500000000000000
Fill: 'off'
LevelList: [1×9 double]
XData: [71×71 double]
YData: [71×71 double]
ZData: [71×71 double]
.
You can carry on working on the contour lines by acquiring the 2D curves in the following way:
x_T_contours=hT_surfc(2).XData
y_T_contours=hT_surfc(2).YData
z_T_contours=hT_surfc(2).ZData
hf(5)=figure(5);contourf(T2,10)
xlabel('D:distance');ylabel('W:depth')
title('Temperature contours with contourf')
% 003
.
Once a surface is available it may be particularly useful to 1: zoom in and out and 2: check data with the cursor, that I prefer call it marker:
.
5.-
Salinity [psu] Surface
.
F_Salinity=scatteredInterpolant(d,w,S)
S2=F_Salinity(D,W)
hf(6)=figure(6);surf(D,W,S2)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['Salinity - interpolation factor Q = ' num2str(Q)])
hf(7)=figure(7);hS_surfc=surfc(D,W,S2)
campos([5578.878486985600 7013.723961265031 21.490607228584])
xlabel('D:distance');ylabel('W:depth')
title('Salinity contour projections')
% 004
x_S_contours=hS_surfc(2).XData;
y_S_contours=hS_surfc(2).YData;
z_S_contours=hS_surfc(2).ZData;
hf(8)=figure(8);contourf(T2,10)
xlabel('D:distance');ylabel('W:depth')
title('Temperature contours with contourf')
6.-
TA [umol/kgSW] surface
F_TA=scatteredInterpolant(d,w,TA)
TA2=F_TA(D,W)
hf(9)=figure(9);surf(D,W,TA2)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['TA - interpolation factor Q = ' num2str(Q)])
hf(10)=figure(10);hTA_surfc=surfc(D,W,S2)
campos([5578.878486985600 7013.723961265031 21.490607228584])
xlabel('D:distance');ylabel('W:depth')
title('TA contour projections')
% 005
x_TA_contours=hTA_surfc(2).XData
y_TA_contours=hTA_surfc(2).YData
z_TA_contours=hTA_surfc(2).ZData
hf(11)=figure(11);contourf(TA2,10)
xlabel('D:distance');ylabel('W:depth')
title('TA contours with contourf')
7.- TCO2 [umol/kgSW] surface
F_TCO2=scatteredInterpolant(d,w,TCO2)
TCO22=F_TCO2(D,W)
hf(12)=figure(12);surf(D,W,TCO22)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['TCO2 - interpolation factor Q = ' num2str(Q)])
hf(13)=figure(13);hTCO2_surfc=surfc(D,W,TCO22)
campos( 1.0e+04 *[ -0.486868459634731 1.534198637901339 0.281087905547732])
xlabel('D:distance');ylabel('W:depth')
title('TCO2 contour projections')
% 006
x_TCO2_contours=hTCO2_surfc(2).XData
y_TCO2_contours=hTCO2_surfc(2).YData
z_TCO2_contours=hTCO2_surfc(2).ZData
hf(14)=figure(14);contourf(TCO22,10)
xlabel('D:distance');ylabel('W:depth')
title('TCO2 contours with contourf')
8.- pCO2 [uatm] surface
F_TpCO2=scatteredInterpolant(d,w,TpCO2)
TpCO22=F_TpCO2(D,W)
hf(15)=figure(15);surf(D,W,TpCO22)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['TpCO2 - interpolation factor Q = ' num2str(Q)])
hf(16)=figure(16);hTpCO2_surfc=surfc(D,W,TpCO22)
campos( 1.0e+04 *[ -0.486868459634731 1.534198637901339 0.281087905547732])
xlabel('D:distance');ylabel('W:depth')
title('TpCO2 contour projections')
% 006
x_pCO2_contours=hTpCO2_surfc(2).XData
y_pCO2_contours=hTpCO2_surfc(2).YData
z_pCO2_contours=hTpCO2_surfc(2).ZData
hf(17)=figure(17);contourf(TpCO22,10)
xlabel('D:distance');ylabel('W:depth')
title('TpCO2 contours with contourf')
9.- Acidity [pH] surface
F_pH=scatteredInterpolant(d,w,pH)
pH2=F_pH(D,W)
hf(18)=figure(15);surf(D,W,pH2)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['pH - interpolation factor Q = ' num2str(Q)])
hf(19)=figure(16);hpH_surfc=surfc(D,W,pH2)
campos( 1.0e+04 *[ -0.486868459634731 1.534198637901339 0.281087905547732])
xlabel('D:distance');ylabel('W:depth')
title('pH contour projections')
% 006
x_pH2_contours=hpH_surfc(2).XData
y_pH2_contours=hpH_surfc(2).YData
z_pH2_contours=hpH_surfc(2).ZData
hf(20)=figure(17);contourf(pH2,10)
xlabel('D:distance');ylabel('W:depth')
title('pH contours with contourf')
10.- Oxygen O2: [umol/kgSW] surface
F_O2=scatteredInterpolant(d,w,O2)
O22=F_O2(D,W)
hf(21)=figure(15);surf(D,W,O22)
campos([4401.80925645085 23683.44745915113 25.18900384688])
xlabel('D:distance');ylabel('W:depth')
title(['O2 - interpolation factor Q = ' num2str(Q)])
hf(22)=figure(16);hO2_surfc=surfc(D,W,O22)
campos( 1.0e+04 *[-0.461106553630150 1.377458945573147 0.001065817992402])
xlabel('D:distance');ylabel('W:depth')
title('O2 contour projections')
% 006
x_O22_contours=hO2_surfc(2).XData
y_O22_contours=hO2_surfc(2).YData
z_O22_contours=hO2_surfc(2).ZData
hf(23)=figure(17);contourf(O22,10)
xlabel('D:distance');ylabel('W:depth')
title('O2 contours with contourf')
.
saving results
.
savefig(hf,'AllFigures.fig') % load with : openfig('AllFigures.fig')
Louis
thanks in advance for time and attention
John BG
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
John BG am 12 Mai 2018
Bearbeitet: John BG am 12 Mai 2018
Hi Louis, on the contrary, some questions jam and additional time is required but I solved similar questions the solving was on schedule.
Happy to help.
Feel free to contact me regarding MATLAB questions with a link to any new question you decide to post so I can have a look.
Katherina Fiege am 17 Jun. 2019
Hi John,
which toolbox does contain the command "acquire measurements"? this is what currently holds my progress with the code you provided.
I'd be very happy if you would answer!
Best,
Kat

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Wick am 11 Mai 2018
This plots the original data, the mesh that interpolates that data, and the contours it creates. If you want it to extrapolate outside of the values of data reported, remove the 'non' from the 'scatteredInterpolant' command.
clearvars
distance = A(:,1);
depth = A(:,2);
pCO2 = A(:,7);
clf
plot3(distance, depth, pCO2,'o');
hold on
x = linspace(min(distance), max(distance), 1000);
y = linspace(min(depth),max(depth), numel(x))';
[XX, YY] = meshgrid(x,y);
F = scatteredInterpolant(distance, depth, pCO2,'linear','none');
ZZ = F(XX,YY);
mesh(XX,YY,ZZ);
contour(XX,YY,ZZ,50)
hold off
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Wick am 11 Mai 2018
simply remove the line with the 'plot' command if you don't want to see the data.
There were three separate plotting commands I used. All can be used alone or together. Those commands are 'plot3', 'contour', and 'mesh.'
You might also try 'pcolor' and 'surf.' I suggest reading about plotting options in the tutorials and help section.
Louis van Herwijnen am 12 Mai 2018
Thank you this helped me a lot !!! thank you again !!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by