Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Single colorbar on figure with subplots?

Subject: Single colorbar on figure with subplots?

From: agkonings@gmail.com

Date: 13 Mar, 2009 14:58:42

Message: 1 of 9

My gut tells me this should be relatively simple, but I can't figure
it out.

I have a figure with four images as subplots (2 by 2). Each has the
same color scaling, so I would like to display all of them with a
single colorbar along the side (or bottom) of the entire figure.
However, the basic colorbar settings seem to force me to choose a
single set of axes (one of the subplots) to place the colorbar near.
Does anybody know how I might get around this?

Thanks for your help!
Alexandra

Subject: Single colorbar on figure with subplots?

From: Bill Comisky

Date: 13 Mar, 2009 17:31:11

Message: 2 of 9

On Mar 13, 10:58=A0am, agkoni...@gmail.com wrote:
> My gut tells me this should be relatively simple, but I can't figure
> it out.
>
> I have a figure with four images as subplots (2 by 2). Each has the
> same color scaling, so I would like to display all of them with a
> single colorbar along the side (or bottom) of the entire figure.
> However, the basic colorbar settings seem to force me to choose a
> single set of axes (one of the subplots) to place the colorbar near.
> Does anybody know how I might get around this?
>
> Thanks for your help!
> Alexandra

Create a new axis just for your colorbar (see axes() command) that
covers your whole figure (overlaps other axes). Make sure you set
caxis() the same for each axis. You can use set() on the output from
axes() to set the 'position' and 'visibility' to what you want. You
might have to tweak the position of your other subplots to make room
if necessary.

Bill

Subject: Single colorbar on figure with subplots?

From: Adam

Date: 12 Apr, 2009 07:30:03

Message: 3 of 9

Bill Comisky <bcomisky@gmail.com> wrote in message <a9fadb42-55fe-49bb-89ee-13cdcede6289@l16g2000yqo.googlegroups.com>...
> On Mar 13, 10:58=A0am, agkoni...@gmail.com wrote:
> > My gut tells me this should be relatively simple, but I can't figure
> > it out.
> >
> > I have a figure with four images as subplots (2 by 2). Each has the
> > same color scaling, so I would like to display all of them with a
> > single colorbar along the side (or bottom) of the entire figure.
> > However, the basic colorbar settings seem to force me to choose a
> > single set of axes (one of the subplots) to place the colorbar near.
> > Does anybody know how I might get around this?
> >
> > Thanks for your help!
> > Alexandra
>
> Create a new axis just for your colorbar (see axes() command) that
> covers your whole figure (overlaps other axes). Make sure you set
> caxis() the same for each axis. You can use set() on the output from
> axes() to set the 'position' and 'visibility' to what you want. You
> might have to tweak the position of your other subplots to make room
> if necessary.
>
> Bill

I have the same problem and your response doesn't make any sense. I've posted this query on the Matlab Forum I'm sorry if a double post has occurred.

Given this simple but working script, could you provide a practical example of what you mean? I couldn't find an actual working example anywhere online.

% begin plotting routine
close all
figure('Color',[1 1 1]);
x1 = 0:12:288;
x2 = 0:15:285;
% define the timesteps of interest
y1 = 1.5:0.044:36.5; % define the y-axis (height in this case)
y2 = [1.5,3.,5.,7.,10.,15.,20.,25.,30.,33.,37.];
[x1,y1] = meshgrid(x1,y1); % build the plot matrix
[x2,y2] = meshgrid(x2,y2); % build the plot matrix
z1 = data_clip(:,114:138); % span of interest
z3 = towerProfile(:,98:117); % select the data to plot
rotate3d
subplot(2,1,1); pcolor(x1,y1,z1)
view([0.5 90]);
shading interp
colormap(jet)
grid off
% Create colorbar
c=colorbar ('FontSize',18);
ylabel(c,'Degrees C')
subplot(2,1,2); pcolor(x2,y2,z3)
axis tight
view([0.5 90]);
shading interp
colormap(jet)
grid off
% Create colorbar
c=colorbar ('FontSize',18);
ylabel(c,'Degrees C')

  

Subject: Single colorbar on figure with subplots?

From: Oliver Woodford

Date: 12 Apr, 2009 12:50:01

Message: 4 of 9

"Adam " <kennaster@gmail.com> wrote:
> Given this simple but working script, could you provide a practical example of what you mean? I couldn't find an actual working example anywhere online.
>
> % begin plotting routine
> close all
> figure('Color',[1 1 1]);
> x1 = 0:12:288;
> x2 = 0:15:285;
> % define the timesteps of interest
> y1 = 1.5:0.044:36.5; % define the y-axis (height in this case)
> y2 = [1.5,3.,5.,7.,10.,15.,20.,25.,30.,33.,37.];
> [x1,y1] = meshgrid(x1,y1); % build the plot matrix
> [x2,y2] = meshgrid(x2,y2); % build the plot matrix
> z1 = data_clip(:,114:138); % span of interest
> z3 = towerProfile(:,98:117); % select the data to plot
> rotate3d
> subplot(2,1,1); pcolor(x1,y1,z1)
> view([0.5 90]);
> shading interp
> colormap(jet)
> grid off
> % Create colorbar
> %c=colorbar ('FontSize',18);
> %ylabel(c,'Degrees C')
> subplot(2,1,2); pcolor(x2,y2,z3)
> axis tight
> view([0.5 90]);
> shading interp
> colormap(jet)
> grid off
> % Create colorbar
axes('Position', [0.05 0.05 0.9 0.9], 'Visible', 'off');
> c=colorbar ('FontSize',18);
> ylabel(c,'Degrees C')

See inline edits.

Subject: Single colorbar on figure with subplots?

From: AMK

Date: 12 Apr, 2009 18:06:51

Message: 5 of 9

Hi Thanks for the edits. This does add the single color bar to reference two sufrace plots in a subplot, but it appears to decouple the colorbar values from the original data. For example, max(z1) and max(z2) are both around 14, but the new single color bar goes from 0 - 80. What is this new colorbar axes referencing?

Thanks,
Adam

Subject: Single colorbar on figure with subplots?

From: Oliver Woodford

Date: 12 Apr, 2009 19:33:01

Message: 6 of 9

AMK <kennaster@gmail.com> wrote:<17701574.29089.1239559642450.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi Thanks for the edits. This does add the single color bar to reference two sufrace plots in a subplot, but it appears to decouple the colorbar values from the original data. For example, max(z1) and max(z2) are both around 14, but the new single color bar goes from 0 - 80. What is this new colorbar axes referencing?
>
> Thanks,
> Adam

It's referencing nothing. Grab the handle to the colorbar and set the limits manually.

Subject: Single colorbar on figure with subplots?

From: AMK

Date: 12 Apr, 2009 21:35:01

Message: 7 of 9

Would this be a setting in the colorbar inspector? I changed the y-scale to 5 - 16, but it just takes the area visible at the previous scale resulting in a color bar that is really just a single color because it's still scaled to 1 - 60.

Subject: Single colorbar on figure with subplots?

From: AMK

Date: 13 Apr, 2009 08:31:08

Message: 8 of 9

Solved. Thanks OW and Pixel at Kluid.com for this one.

A(1)=subplot(2,1,1); mesh(x,y,z)
A(2)=subplot(2,1,2); mesh(x,y,z)
B=colorbar;
set(B, 'Position', [.8314 .11 .0581 .8150])
for i=1:2
pos=get(A(i), 'Position');
axes(A(i))
set(A(i), 'Position', [pos(1) pos(2) .6626 pos(4)])
end

Subject: Single colorbar on figure with subplots?

From: Eric

Date: 19 Feb, 2011 20:51:24

Message: 9 of 9

AMK <kennaster@gmail.com> wrote in message <16583380.29435.1239572132296.JavaMail.jakarta@nitrogen.mathforum.org>...
> Would this be a setting in the colorbar inspector? I changed the y-scale to 5 - 16, but it just takes the area visible at the previous scale resulting in a color bar that is really just a single color because it's still scaled to 1 - 60.

Use the property 'Clim' to set colorbar limits for all subplots.
e.g.:

for cz = 1:8
    % Create subplot
    subplot1(cz) = subplot(2,4,cz,'Parent',figure1,'CLim',[0 8]);
    
    % Create contour
    contour(x,y,values_19(:,:,cz),'ShowText','on','LineWidth',1,'LineStyle','none',...
    'Fill','on',...
    'LevelStep',0.5,...
    'DisplayName','values(:,:,1)',...
    'Parent',subplot1(cz));

end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us