How to place a common colorbar for tiledlayout plots?

I wanted to place a large common colorbar towards "east" for a series of plots using tiledlayout. I used the following code:
figure;
t = tiledlayout(2,2,'TileSpacing','none','Padding','none');
nexttile;
imagescn(x,y,A);
cb = colorbar;
hold on;
stipple(x',y',A1);
mymap.EdgeAlpha=0; %Removes the grids
colorbar;
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
% To set label to colorbar
h = colorbar;
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('A','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
cb = colorbar;
cmocean('balance','pivot') % sets the colormap with zero in the middle
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
hold on;
geoshow(shapefile, 'FaceColor', 'none','LineWidth',1); %No facecolor
set(cb,'FontSize',10)
nexttile;
imagescn(x,y,B);
cb = colorbar;
hold on;
stipple(x',y',B1);
mymap.EdgeAlpha=0; %Removes the grids
colorbar;
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
% To set label to colorbar
h = colorbar;
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('B','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
cb = colorbar;
cmocean('balance','pivot') % sets the colormap with zero in the middle
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
hold on;
geoshow(shapefile, 'FaceColor', 'none','LineWidth',1); %No facecolor
set(cb,'FontSize',10)
nexttile;
imagescn(x,y,C);
cb = colorbar;
hold on;
stipple(x',y',C1);
mymap.EdgeAlpha=0; %Removes the grids
colorbar;
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
% To set label to colorbar
h = colorbar;
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('C','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
cb = colorbar;
cmocean('balance','pivot') % sets the colormap with zero in the middle
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
hold on;
geoshow(shapefile, 'FaceColor', 'none','LineWidth',1); %No facecolor
set(cb,'FontSize',10)
nexttile;
imagescn(x,y,D);
cb = colorbar;
hold on;
stipple(x',y',D1);
mymap.EdgeAlpha=0; %Removes the grids
colorbar;
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
% To set label to colorbar
h = colorbar;
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('D','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
cb = colorbar;
cmocean('balance','pivot') % sets the colormap with zero in the middle
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
This code is giving individual colorbars for each plot. But I want a common colorbar for all the plots together towards the eastern end. How to do it?

 採用された回答

Matt J
Matt J 2022 年 4 月 24 日

0 投票

tiledlayout(2,2)
nexttile; nexttile; nexttile; nexttile
cb = colorbar;
cb.Layout.Tile = 'east';

8 件のコメント

Abhishek Chakraborty
Abhishek Chakraborty 2022 年 4 月 24 日
Hi I tried the same, but when I am trying to keep the colorbar patter to "cmocean", only the last plot is following that colormap pattern. How to get the same colorbar format (cmocean) for all the plots? The code I used is this:
figure;
t = tiledlayout(2,2,'TileSpacing','compact','Padding','compact');
nexttile;
imagescn(x,y,A);
hold on;
stipple(x',y',A1);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
title('A','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
nexttile;
imagescn(x,y,B);
hold on;
stipple(x',y',B1);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
title('B','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
nexttile;
imagescn(x,y,C);
hold on;
stipple(x',y',C1);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('C','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
nexttile;
imagescn(x,y,D);
hold on;
stipple(x',y',D1);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('D','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
cb = colorbar;
cb.Layout.Tile = 'east';
>> cmocean('balance','pivot') % sets the colormap with zero in the middle
And I am getting this plot: (I need all the plots following the cmocean colorbar format)
Matt J
Matt J 2022 年 4 月 24 日
編集済み: Matt J 2022 年 4 月 24 日
The code can't be run standalone. It's missing input variables.
Abhishek Chakraborty
Abhishek Chakraborty 2022 年 4 月 24 日
Hi Matt. Sorry for not giving the input variables. I have given it now in the following code:
x=68.2250:0.05:97.3750;
y=36.9750:-0.05:6.8250;
A=rand(604,584)-0.5;
B=rand(604,584)-0.5;
C=rand(604,584)-0.5;
D=rand(604,584)-0.5;
figure;
t = tiledlayout(2,2,'TileSpacing','compact','Padding','compact');
nexttile;
imagescn(x,y,A);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
title('A','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
nexttile;
imagescn(x,y,B);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
title('B','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
nexttile;
imagescn(x,y,C);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('C','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
nexttile;
imagescn(x,y,D);
mymap.EdgeAlpha=0; %Removes the grids
box on;
set(gca,'linew',2); % thickens the axis
axis equal; %important for maps so that there is no stretch
set(gca,'XMinorTick','on','YMinorTick','on');
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
%Axis ticks bolded
set(gca,'TickLabelInterpreter','none');
set(gca,'fontweight','bold','fontsize',12);
title('D','FontWeight','bold','FontSize',14,'FontName','Helvetica'); % title bold and size 14
set(gca,'xticklabel',{[]});set(gca,'yticklabel',{[]});
cb = colorbar;
cb.Layout.Tile = 'east';
cmocean('balance','pivot') % sets the colormap with zero in the middle
Matt J
Matt J 2022 年 4 月 24 日
Nope:
x=68.2250:0.05:97.3750;
y=36.9750:-0.05:6.8250;
A=rand(604,584)-0.5;
B=rand(604,584)-0.5;
C=rand(604,584)-0.5;
D=rand(604,584)-0.5;
figure;
t = tiledlayout(2,2,'TileSpacing','compact','Padding','compact');
ax(1)=nexttile;
imagescn(x,y,A);
Unrecognized function or variable 'imagescn'.
Abhishek Chakraborty
Abhishek Chakraborty 2022 年 4 月 24 日
It worked for me perfectly and is showing this plot:
Abhishek Chakraborty
Abhishek Chakraborty 2022 年 4 月 24 日
編集済み: Matt J 2022 年 4 月 24 日
Matt J
Matt J 2022 年 4 月 24 日
編集済み: Matt J 2022 年 4 月 24 日
You must apply cmocean in all the nexttile blocks. Currently you are only doing so in the last block.
Abhishek Chakraborty
Abhishek Chakraborty 2022 年 4 月 24 日
It worked. Thanks a lot .

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by