How can I set the width of a COLORBAR in a figure in MATLAB 7.0 (R14)?

106 views (last 30 days)
I create a figure by typing the following at the MATLAB command prompt:
pcolor(peaks);
c=colorbar;
Now I try to set the width of the colorbar as:
x=get(c,'Position');
x(3)=0.03;
set(c,'Position',x)
However, this results in the colorbar in the middle of the figure window.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to manually set the width of a colorbar without changing its position is unavailable in MATLAB.
To workaround this issue, type the following:
pcolor(peaks);
c=colorbar;
x1=get(gca,'position');
x=get(c,'Position');
x(3)=0.03;
set(c,'Position',x)
set(gca,'position',x1)
  1 Comment
Kevin Gleason
Kevin Gleason on 27 Sep 2016
You will need to manage the various axes of the figure. Then, "colorbar" can take an axes as argument.
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
pcolor(ax1, peaks);
pcolor(ax2,-peaks);
c1 = colorbar(ax1);
c2 = colorbar(ax2);
x1 = get(ax1, 'position'); % get axes position
cw1= get(c1, 'Position'); % Colorbar Width for c1
% Set position of axes and
From there the modifications and set command from the original answer should work.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!