How to set common colorbar for multiplots?

Hi all,
I am using subplots to plot multiple contour plots in a single figure using for loop and using colorbar with 'Position' but it doesn't give the common values for all the plots. I have tried different solution which was given to other Matlab users but none worked.
fig=figure(1)
clf
for i=1:24
subplot(6,4,i)
contourf(x,y,z)
end
h=axes(fig,'visible','off');
h.Title.Visible='on';
h.XLabel.Visible='on';
h.YLabel.Visible='on';
ylabel(h,'yaxis','FontWeight','bold');
xlabel(h,'xaxis','FontWeight','bold');
title(h,'title');
colormap(jet)
c=colorbar;
c.Position = [0.93 0.168 0.022 0.7];
Can you help me with this problem?
Thanks in advance!

 採用された回答

Robert U
Robert U 2021 年 9 月 1 日

10 投票

Hi UTKARSH VERMA,
have a look at caxis():
[x, y] = meshgrid(0:0.1:1,0:0.1:1);
z = rand(11,11,24);
minColorLimit = min(min(min(z))); % determine colorbar limits from data
maxColorLimit = 24*max(max(max(z)));
fig=figure(1);
for i=1:24
sph{i} = subplot(6,4,i,'Parent',fig);
contourf(sph{i},x,y,i.*z(:,:,i)) % changes due to illustration purposes
caxis(sph{i},[minColorLimit,maxColorLimit]); % set colorbar limits
end
h = axes(fig,'visible','off');
h.Title.Visible = 'on';
h.XLabel.Visible = 'on';
h.YLabel.Visible = 'on';
ylabel(h,'yaxis','FontWeight','bold');
xlabel(h,'xaxis','FontWeight','bold');
title(h,'title');
c = colorbar(h,'Position',[0.93 0.168 0.022 0.7]); % attach colorbar to h
colormap(c,'jet')
caxis(h,[minColorLimit,maxColorLimit]); % set colorbar limits
Kind regards,
Robert

7 件のコメント

UTKARSH VERMA
UTKARSH VERMA 2021 年 9 月 1 日
Thanks Robert!!
It's working perfectly.
Arnav Gupta
Arnav Gupta 2022 年 7 月 1 日
Hey robert I executed this code but noticed that once a single colorbar comes for all subplots no matter how much i change its limits , there is no change in the subplots. Is it becuase the subplots limits are already specified in for loop and outer colorbar limits has no relation with these subplots??
Robert U
Robert U 2022 年 8 月 23 日
Hi Arnav Gupta,
exactly. The created colorbar is an individual object on the figure and will not change no matter what you do with you subplots. In order to change that you would have to add a listener to the colorbar that will recalculate on colormap changes within the subplots. That's far more work than recalculating the whole figure and adjust the colorbar limits.
Kind regards,
Robert
Ashfaq Ahmed
Ashfaq Ahmed 2023 年 5 月 15 日
How can I remove the xtics and yticks? @Robert U
Ashfaq Ahmed
Ashfaq Ahmed 2023 年 5 月 15 日
I am writing this code -
%load NBayTEMPdetmnth.mat
clf;
fig=figure(1);
t = tiledlayout(4,3); t.TileSpacing = 'compact';
for i = 1:12
nexttile
[~, hContour]=contourf(flip(NBayTEMPdetmnth(:,:,i)),25,'-','linewidth',0.5);
hold on;
end
h = axes(fig,'visible','off');
h.Title.Visible = 'off';
h.XLabel.Visible = 'off';
h.YLabel.Visible = 'off';
c = colorbar(h,'Position',[0.93 0.168 0.022 0.7]); % attach colorbar to h
colormap(c,'jet')
caxis(h,[-5 20]); % set colorbar limits
Robert U
Robert U 2023 年 5 月 16 日
Try this:
NBayTEMPdetmnth = rand([256,256,12]);
clf;
fig=figure(1);
t = tiledlayout(4,3); t.TileSpacing = 'compact';
for i = 1:12
ah = nexttile;
[~, hContour]=contourf(ah,flip(NBayTEMPdetmnth(:,:,i)),25,'Linestyle','none','linewidth',0.5);
ah.XTick = [];
ah.YTick = [];
end
h = axes(fig,'visible','off');
h.Title.Visible = 'off';
h.XLabel.Visible = 'off';
h.YLabel.Visible = 'off';
c = colorbar(h,'Position',[0.93 0.168 0.022 0.7]); % attach colorbar to h
colormap(c,'jet')
caxis(h,[-5 20]); % set colorbar limits
Kind regards,
Robert
Shuchi
Shuchi 2023 年 6 月 13 日
Thanks for the code.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2023 年 6 月 20 日
編集済み: Adam Danz 2023 年 6 月 20 日

5 投票

This is easy with tiledlayout (MATLAB R2020b or later)
tcl = tiledlayout(3,4);
for i = 1:prod(tcl.GridSize)
nexttile()
[X,Y,Z] = peaks(2+i);
contourf(X,Y,Z)
clim([-7,6]) % Important! Set the same color limits
end
cb = colorbar();
cb.Layout.Tile = 'east'; % Assign colorbar location

2 件のコメント

Diana Grisolia
Diana Grisolia 2024 年 4 月 11 日
Thnks! It worked perfectly!!
Amirmohammad
Amirmohammad 2026 年 1 月 23 日
Perfect solution for me too! Thanks!

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

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by