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.

REQUIREMENTS

- Letter size of smallest letters approx. 2.4 mm
- Grids
- Legend on top of diagram
- Width of diagram 72 mm when printed

set(gcf,'PaperUnits','centimeters','PaperPosition',[1 1 7.2 5.0]) set(gcf,'Units','centimeters','position',get(gcf,'PaperPosition'))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.

set(gcf,'DefaultAxesFontSize',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,

x=0:.1:6*pi; plot(x,sin(x),x,cos(x)) title('Analysis of the phase lag between Sin and Cos') xlabel('Radians'); ylabel('Amplitude');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 set(gca,'position',pos);That is better. Now, let's put a grid and a legend on it.

grid on legend('Sin','Cos')The grid lines now are obscuring the legend. Rrrr... A quick look at the help for LEGEND suggests a fix,

h = legend('Sin','Cos'); axes(h); refresh;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,

Other things you might want to do are: Change the line width,

h = plot(x,sin(x),x,cos(x)); set(h,'linewidth',3)change the tick locations,

set(gca,'xtick',[0 3 7 13 17])change the font used,

set(gca,'fontname','times-roman')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

axes(gco)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