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

8 visualizaciones (últimos 30 días)
Ashfaq Ahmed
Ashfaq Ahmed el 12 de Dic. de 2023
Comentada: Voss el 12 de Dic. de 2023
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!!

Respuesta aceptada

Voss
Voss el 12 de Dic. de 2023
load SALT
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 comentarios
Ashfaq Ahmed
Ashfaq Ahmed el 12 de Dic. de 2023
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';
t.Padding = 'compact';
xIdx = 30;
yIdx = 75;
% Plotting
nexttile
contourf(rot90(rot90(SALT(:,:,1))),150,'edgecolor','none'); shading interp;
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
Voss el 12 de Dic. de 2023
Yes, you can make a function, but a loop may be good enough:
load SALT
%% 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';
t.Padding = 'compact';
xIdx = 30;
yIdx = 75;
% Plotting
nexttile
contourf(rot90(rot90(SALT(:,:,1))),150,'edgecolor','none'); shading interp;
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';

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by