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:


figure(1)
  caxis([0,50])            %change the range
  colorbar()
  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',
    'ticklength',[0.005,0.005],
    'ytick', [0, 10, 23, 42, 50],
    'yticklabel',{'zero','ten','23','42','fifty'})


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)









1 comment:

  1. Hi, thanks for providing these details about octave's colorbars, that really helped me.

    Is there a reason why you used
    "findobj"
    instead of directly assigning a handle via
    "cbh = colorbar"
    ?

    ReplyDelete