1

I would like to reposition the colorbar title (not the colorbar itself). I tried using the following code:

cb = colorbar;                                    
set(get(cb,'title'),'string','Dislocation Density(m^{-2})'); 

lbpos = get(cb,'title');
pos = get (lbpos,'position'); 
set(lbpos, 'position', pos) ;

I have got three problems:

  • there are four numbers in the vector pos and I don't know which number should I modified in order to move the title upward so that it would not overlap the colorbar.
  • I have tried to alter some number in pos but whatever I do the title just move down and overlap with the colorbar.
  • In addition, when I try to enlarge the whole graph with the maximize button at the upper right corner, the position of the colorbar title also changes. This doesn't happen if I don't try to re-position the colorbar title.
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Kelvin S
  • 341
  • 2
  • 4
  • 13

1 Answers1

1

According to the documentation on text object, the 'Position' of a text object only has 3 numbers [x,y,z] when the 'Units' is data, which is also the default setting. So I think your colorbar text is somehow modified. Let's try this:

cb = colorbar;                                    
set(get(cb,'title'),'string','Dislocation Density(m^{-2})'); 
lbpos = get(cb,'title');

% change Units to data
set(lbpos,'Units','data');
% get position, should have 2 or 3 values
pos = get (lbpos,'position'); 
% move up a bit
pos(2) = pos(2)+3;
set(lbpos, 'position', pos);
MinF
  • 326
  • 1
  • 4
  • I have tried your method, but even I use pos(2)+300 the colorbar title doesn't move at all. Which number of the vector "pos" represent the position of the colorbar title? – Kelvin S Oct 06 '15 at 02:56
  • Have you changed the Units to data ? According to the [documentation](http://de.mathworks.com/help/matlab/ref/text.html#input_argument_namevalue_position), the vector 'position' is [x,y] or [x,y,z], so all number define the position of the colorbar title (**'Units'** is **data**). If so try set(lbpos, 'position', pos(1:2)); as vector of 2 should work. I think setting pos with a vector of 4 causes your problem – MinF Oct 06 '15 at 06:26
  • I tried to use the following: cb = colorbar; set(get(cb,'title'),'string','Dislocation Density(m^{-2})'); lbpos = get(cb,'title'); set(lbpos,'Units','data'); pos = get (lbpos,'position'); pos(2) = pos(2)-50; set(lbpos, 'position', pos(1:2)); But it still doesn't work. The MATLAB version I use is 2014a. – Kelvin S Oct 06 '15 at 07:21
  • hmm I run your script and it works. See [screenshot](http://i.stack.imgur.com/H17ME.png). I use Matlab r2013a – MinF Oct 06 '15 at 07:49
  • 1
    Try this one: `set(lbpos,'units','normalized','position',[0,1.03]);` . It works for me as well – MinF Oct 06 '15 at 07:58
  • Or maybe try this one too: `set(lbpos,'Parent',cb);`. My advise is too run `get(lbpos)` before and after modifying anything to see if anything went wrong – MinF Oct 06 '15 at 08:05
  • The command: : set(lbpos,'units','normalized','position',[0,1.03]); works for me. Thank you. – Kelvin S Oct 06 '15 at 08:13
  • I don't know why other commands cannot work. In addition, even I use set(lbpos,'units','normalized','position',[0,1.03]); the space between the colorbar and its title changes when I maximize the figure's dialogue. Which one is the "correct" figure, the maximized or non-maximized? – Kelvin S Oct 06 '15 at 08:16
  • 1
    In general, the Units `normalized` means it is normalized to the parent container. So i think it's normal to observe some changes in the distance when you maximize the figure, although it is not that clear to me (maybe because I have small laptop screen). If you wish, you can look at the [documentation](http://de.mathworks.com/help/matlab/ref/text.html#input_argument_namevalue_position) and play around with different setting of `Units` and `Position` – MinF Oct 06 '15 at 08:24