# How to plot a 2D filled colour contour plot depth profile?

조회 수: 16 (최근 30일)
Ashfaq Ahmed 2023년 12월 12일
댓글: Voss 2023년 12월 12일
Hi all,
I have a 3D matrix named SALT which is of 90x90x50 size. Here, 90x90 is latitude/longitude information and 50 is salinity informatin in each ocean layer depth. I want to plot an intersected salinity contour profile like this -
The intersection will go through the middle of the map, or anywhere according to my choice. I have attached the 3D matrix in this question. Can anyone please help me out here?
Any feedback will be greatly appreciated!!

댓글을 달려면 로그인하십시오.

### 채택된 답변

Voss 2023년 12월 12일
s = permute(SALT(35,:,:),[3 2 1]);
contourf(s)
set(gca(),'YDir','reverse')
xlabel('Longitude(?) Index')
ylabel('Depth Index')
title('Salinity Profile at Latitude(?) #35')
ylabel(colorbar(),'Salinity')
##### 댓글 수: 2없음 표시없음 숨기기
Ashfaq Ahmed 2023년 12월 12일
Great! I actually did some brain storming and ended up with this plot -
I used these lines of code -
%% Salinity
clr = flipud(brewermap(15,'BrBG'));
clri= interp1(1:1:15,clr,1:0.25:15,'linear');
figure(1),clf;
n = 1;
t = tiledlayout(1,3);
t.TileSpacing = 'tight';
xIdx = 30;
yIdx = 75;
% Plotting
nexttile
colormap(gca, clri);
set(gca,'color',[0.55 0.55 0.55]);
line([xIdx, xIdx], [1, 90], 'Color', 'b', 'LineWidth', 4); hold on;
line([1, 90], [yIdx, yIdx], 'Color', 'r', 'LineWidth', 4); hold on;
set(gca, 'GridLineStyle', '-', 'MinorGridLineStyle', ':', 'LineWidth', 3);
box on; set(gca, 'GridLineStyle', '-','LineWidth', 3);
hold on; set(gca, 'Layer', 'top');
xlabel('x-index'); ylabel('y-index')
set(gca,'XTickLabel'); set(gca,'YTickLabel');
set(gca,'FontSize', 15)
title('Intersection line on Arctic')
nexttile
% Extract the salinity profile at the chosen latitude
salinityProfile = squeeze(SALT(:,90-xIdx,:));
salinityProfile =salinityProfile';
salinityProfile = fliplr(salinityProfile);
contourf(salinityProfile,15,'edgecolor','k','linewidth',1.5);
colormap(gca, clri)
set(gca, 'YDir', 'reverse');
set(gca,'color',[0.55 0.55 0.55])
set(gca, 'GridLineStyle', '-', 'MinorGridLineStyle', ':', 'LineWidth', 3);
box on; set(gca, 'GridLineStyle', '-','LineWidth', 3);
hold on; set(gca, 'Layer', 'top');
title('Salinity with depth (blue intersect)');
set(gca,'XTickLabel',[]); set(gca,'YTickLabel');
ylabel('Depth (m)');
set(gca,'FontSize', 15)
nexttile
% Extract the salinity profile at the chosen latitude
salinityProfile = squeeze(SALT(90-yIdx,:,:));
salinityProfile =salinityProfile';
salinityProfile = fliplr(salinityProfile);
contourf(salinityProfile,15,'edgecolor','k','linewidth',1.5);
colormap(gca, clri)
set(gca, 'YDir', 'reverse');
set(gca,'color',[0.55 0.55 0.55])
set(gca, 'GridLineStyle', '-', 'MinorGridLineStyle', ':', 'LineWidth', 3);
box on; set(gca, 'GridLineStyle', '-','LineWidth', 3);
hold on; set(gca, 'Layer', 'top');
title('Salinity with depth (red intersect)');
set(gca,'XTickLabel',[]); set(gca,'YTickLabel');
ylabel('Depth (m)');
set(gca,'FontSize', 15)
set(gcf, 'Position', [-2348,775,1403,476]);
c = colorbar;
c.FontSize = 15;
c.FontWeight = 'bold';
c.Layout.Tile = 'east';
I had a relevant quetsion. As you can see I had to use the same lines for the plotting repeatedly. Can I make them a plotting function and reduce my coding lines significantly? Can you help me ouit here? @Voss
Voss 2023년 12월 12일
Yes, you can make a function, but a loop may be good enough:
%% Salinity
clr = flipud(parula(15));
clri= interp1(1:1:15,clr,1:0.25:15,'linear');
figure(1),clf;
n = 1;
t = tiledlayout(1,3);
t.TileSpacing = 'tight';
xIdx = 30;
yIdx = 75;
% Plotting
nexttile
colormap(gca, clri);
set(gca,'color',[0.55 0.55 0.55]);
line([xIdx, xIdx], [1, 90], 'Color', 'b', 'LineWidth', 4); hold on;
line([1, 90], [yIdx, yIdx], 'Color', 'r', 'LineWidth', 4); hold on;
set(gca, 'GridLineStyle', '-', 'MinorGridLineStyle', ':', 'LineWidth', 3);
box on; set(gca, 'GridLineStyle', '-','LineWidth', 3);
hold on; set(gca, 'Layer', 'top');
xlabel('x-index'); ylabel('y-index')
set(gca,'XTickLabel',[],'YTickLabel',[]);
set(gca,'FontSize', 15)
title('Intersection line on Arctic')
data = {SALT(:,90-xIdx,:) SALT(90-yIdx,:,:)};
names = {'blue' 'red'};
for ii = 1:numel(data)
nexttile
% Extract the salinity profile at the chosen latitude or longitude
salinityProfile = squeeze(data{ii});
salinityProfile =salinityProfile';
salinityProfile = fliplr(salinityProfile);
contourf(salinityProfile,15,'edgecolor','k','linewidth',1.5);
colormap(gca, clri)
set(gca, 'YDir', 'reverse');
set(gca,'color',[0.55 0.55 0.55])
set(gca, 'GridLineStyle', '-', 'MinorGridLineStyle', ':', 'LineWidth', 3);
box on; set(gca, 'GridLineStyle', '-','LineWidth', 3);
hold on; set(gca, 'Layer', 'top');
title(sprintf('Salinity with depth (%s intersect)',names{ii}));
set(gca,'XTickLabel',[],'YTickLabel',[]);
ylabel('Depth (m)');
set(gca,'FontSize', 15)
end
set(gcf, 'Position', [-2348,775,1403,476]);
c = colorbar;
c.FontSize = 15;
c.FontWeight = 'bold';
c.Layout.Tile = 'east';

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by