How to set common colorbar for multiplots? (2024)

880 views (last 30 days)

Show older comments

UTKARSH VERMA on 1 Sep 2021

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots

Commented: Diana Grisolia on 11 Apr 2024 at 11:51

Accepted Answer: Robert U

Open in MATLAB Online

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!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Robert U on 1 Sep 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#answer_778664

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#answer_778664

Open in MATLAB Online

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

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

How to set common colorbar for multiplots? (3)

Kind regards,

Robert

7 Comments

Show 5 older commentsHide 5 older comments

UTKARSH VERMA on 1 Sep 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_1715874

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_1715874

Thanks Robert!!

It's working perfectly.

Arnav Gupta on 1 Jul 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2246095

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2246095

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 on 23 Aug 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2327960

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2327960

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 on 15 May 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2747354

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2747354

How can I remove the xtics and yticks? @Robert U

Ashfaq Ahmed on 15 May 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2747359

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2747359

Open in MATLAB Online

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 on 16 May 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2748234

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2748234

Open in MATLAB Online

Hi @Ashfaq Ahmed,

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

How to set common colorbar for multiplots? (10)

Kind regards,

Robert

Shuchi on 13 Jun 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2780534

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_2780534

Thanks for the code.

Sign in to comment.

More Answers (1)

Adam Danz on 20 Jun 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#answer_1259339

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#answer_1259339

Edited: Adam Danz on 20 Jun 2023

Open in MATLAB Online

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

How to set common colorbar for multiplots? (13)

1 Comment

Show -1 older commentsHide -1 older comments

Diana Grisolia on 11 Apr 2024 at 11:51

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_3128406

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1444554-how-to-set-common-colorbar-for-multiplots#comment_3128406

Thnks! It worked perfectly!!

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationColormapsOrange

Find more on Orange in Help Center and File Exchange

Tags

  • common_colorbar subplots

Products

  • MATLAB

Release

R2018a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to set common colorbar for multiplots? (15)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to set common colorbar for multiplots? (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5807

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.