May 20, 2012

high quality plots in octave

When you're about to publish in a scientific journal, one problem appears: how do I create high quality plots for my paper?


You can use all kinds of expensive programs (Origin, for example), but free software is as well capable of doing this. For print, you definitely want to use some vector based image formats, of which EPS (encapsulated postscript) is the most common.

Well let's look at a simple eps, created with octave:

generic EPS plot. we have to work on this.
 code for this example is below
This looks quite poor:

  • the proportions of data points, axis, ticks are not good
  • the fonts look messy
  • there is no color
The good news is: we definitely can improve this. The bad news: i tried the example on different machines and depending on the octave version and the installed postscript drivers / conversion tools, the results differ. So, as always the parola is: play with the given values :)

A good idea is to look whether the journal you're intending to publish in has any style guidelines. Often, detailed information about the size, font, and axis labels can be found there.

A list of task that can be done to improve the above plot:

  • we'll use colors
  • we'll use another font: a sans-serif is standard in scientific plots (e.g. Helvetica or Arial). The font size will be changed.
  • we'll change the proportions of the image. The data points and lines are the most important issue on our plot, so we'll make them quite big and fat
  • the outer proportions of the image are changed. In a two-column layout which is common in many publications, often a width-to-height ratio of 2:1 is demanded
  • we'll  make the axis a little thicker. In some cases, it is desirable to have the ticks outside
  • Often, the axis are heavily overloaded, there is too much information distracting from the plot content. This can be reduced by leaving some ticklabels out.
The result looks more nicely:

an improved version.


I hope this is a good starting point. The above pictures where created on an Ubuntu 10.04 machine with octave 3.6.x. Now the code:


clear all;
more off;
%
% create some data: polynoms with random noise
%
points = 15
x = linspace(-5,5,points);
y1 = 0.3*x.^2 + 5 *(rand(1,points)-.5)+15;
y2 = 0.1*x.^3 + 5 *(rand(1,points)-.5);
%
% fit some curves to the noisy data
%
[p1,s1] = polyfit(x,y1,3);
[p2,s2] = polyfit(x,y2,3);
tx = linspace(min(x),max(x),500)
%
% create a generic plot
%
figure(1)
plot(  x,y1,'x',
  x,y2,'o',
  tx,polyval(p1,tx),
  tx,polyval(p2,tx))
xlabel 'X'
ylabel 'Y'
legend('y1',
'y2',
'fit1',
'fit2')
print('generic.eps')


%
% create a more advanced version of the plot
figure(2)
%
% in the plot command
% - define markersize
% - define linewidth
% - assign colors
%
plot(  x,y1,'x'
,'linewidth',6
,'color',[1,0,0]
,'markersize',6
  ,x,y2,'o'
  ,'color',[0,0,1]
  ,'markerfacecolor',[0,0,1]
  ,'markersize',6,
  tx,polyval(p1,tx),
  'linewidth',3,
  'color',[1,0,0],
  tx,polyval(p2,tx),
  'linewidth',3,
  'color',[0,0,1])
%
% now let's adjust the axis:
% - make the line thicker
% - make the ticks outside
% - adjust ticklength
% - use custom ticks
% - apply custom ticklabels
%
set(gca(),
'linewidth',2,
'tickdir','out',
'ticklength',[0.005,0.005],
'xtick',[-6:6],
'xticklabel',{'','-5','','','','','0','','','','','5',''},
'ytick',[-20:10:30],
'ylim',[-20,30],
'yticklabel',{'','-20','','0','','20',''})   
xlabel 'X'
ylabel 'Y'
%
% the legend shall appera on the bottom right of the plot
%
legend('y1','y2','fit1','fit2','location','southeast')

% let's print this thing to a eps.
% we use a custom size of 900x450,
% a defined Font and Size
% and the depsc2 driver (EPS 2 color) driver
print('test2.eps','-S900,450',
'-depsc2',
'-F:Helvetica:12',
'-tight'
)


I guess this is a good starting point for creating printable plots that look good. When you're not sure what can be done: Have a look at the attributes of your graphic and axis objects:

get( gcf())   %get current figure
get( gca())  %get current axis

you can set it with the set command

set( gca(), 'property','value')

Have a look at the octave manual. The mathworks website can also be useful, sometimes ;-)