Q: How to format a graph for inclusion in documents?

(© Drea Thomas, MathWorks newsletter, June 1995)

Formatting Graphs for Inclusion in Documents.

A common question we get is how to format a graph for inclusion in a report or scientific journal. I'll take one specific set of requirements and show how to tune a plot using Handle Graphics to meet them. I'll try to make 2-D diagrams according to the "AIP Style Manual", (American Inst. of Physics), for the journal Solar Energy.


If you want to control exactly how a plot is going to look on paper, it is a good idea to WYSIWYG (what you see is what you get) the figure window. That is, set the figure window 'Units' property to physical units, and set the figure window size the same as the 'PaperPosition' size. For our case,
set(gcf,'PaperUnits','centimeters','PaperPosition',[1 1 7.2 5.0]) 
Note that I've offset the lower left-hand corner of the graph by 1 cm in X and Y because most printers can't physically print to the edge of piece of paper. If you specified a [0 0] offset, the plot would be cut off. Font sizes are usually measured in points. Since there are about 2.8 points per mm, this means the minimum fontsize is 6 or 7. Let's set the default fontsize for the figure window to 8.
See Drea's Desk, April 1994 for a discussion of default properties (ftp://ftp.mathworks.com/pub/doc/tmw-digest/apr94). Put up an incredibly profound plot, with Title, X and Y labels,
title('Analysis of the phase lag between Sin and Cos') 
It is immediately apparent that the default axis size for this plot is too large (the labels get cut off). I'll shrink the size of the axis within the figure window,
pos = get(gca,'position'); % This is in normalized coordinates 
pos(3:4)=pos(3:4)*.9; % Shrink the axis by a factor of .9 
pos(1:2)=pos(1:2)+pos(3:4)*.05 % Center it in the figure window 
That is better. Now, let's put a grid and a legend on it.
grid on 
The grid lines now are obscuring the legend. Rrrr... A quick look at the help for LEGEND suggests a fix,
h = legend('Sin','Cos'); 
The reason for this strange behavior is that legend is an axis. Unless it is the current axes, it is drawn "behind" the other axis (i.e., your plot). So, just before you print, make it the current axis and it will appear "on top" (you wouldn't want to always make it the current axis because then the next PLOT command would go into the legend). Now print it out,
If all goes well, you have a printout that exactly corresponds to the style requirements.
Other things you might want to do are: Change the line width,
h = plot(x,sin(x),x,cos(x)); 
change the tick locations,
set(gca,'xtick',[0 3 7 13 17]) 
change the font used,
etc. Note that if you didn't clear the figure window, you now have a very ugly plot because these commands affected the legend not the plot (remember the warning about making legend the current axis?). To change the current axis, use this handy trick, Click on the axis you want to make current (on the box itself), then
This sets the current axis to the current object (the last thing clicked on). If you find yourself often formatting graphs in a particular way, it might be wise to write a M-file to automate the process. Manipulating objects using Handle Graphics can be a little tricky at first but once you understand the paradigm, you find that it gives you control over nearly every aspect of MATLAB plots. I hope this has been useful. Thanks for visiting my desk. Drea Thomas drea@mathworks.com