April 21, 2012

octave quickies: customizing the colorbar

When you create colormap or 3D-plots, you may wish to customize the axis of your plot (e.g. change the linewidth, etc). One more difficult task  is to change the colorbar of the plot.

you should customize the colorbar to your needs!

As in matlab, in octave things like lines, plot points, axis and so on are basically graphical objects (see the octave manual). To change the properties of some object, you have to know its handle (although for some, e.g. xlim, there are special function to do so).

One thing I stumble across regularly is the colorbar, where i want to change the linewidth and the ticks. The octave manual is not very verbose at this point, but you basically can use the matlab doc.

Example: we create a blank plot with a colorbar. Then we aquire the colorbar handle and play around with the colorbar properties:

  caxis([0,50])            %change the range
  colormap( summer(250))   %use some different map than jet

                           %get the  colorbar handle
  cbh = findobj( gcf(), 'tag', 'colorbar')
  set( cbh,                %now change some colorbar properties
    'linewidth', 2,
    'tickdir', 'out',
    'ytick', [0, 10, 23, 42, 50],

As for all objects, you get get a complete list of the actual colorbars properties with the get function:
get( cbh) 

Example : you can change the dimensions (position, height, width) of the colorbar using the position property:

%get the standard value
octave-3.4.2:8> get(cbh,'position')
ans = 0.827500   0.110000   0.046500   0.815000
%set new value
set(cbh,'position',[0.8275 0.11, 0.02325 0.81500]) 
%(makes a slicker colorbar)