March 11, 2005

A few helpful tips on plotting in Matlab

Every few months I find myself having the same questions on plotting in Matlab. It's time to stop being so unproductive. Here are a few tips for my future reference and yours.

Automating "Fill page". How many times have you clicked "fill page" under Page Setup. If you are like me, the answer is many times too many. To automate this, you need to set the PagePosition property of the current figure.

set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperPosition', [.25 .25 8 10.5]);
If you need to switch to landscape (the default is portrait), you will want to edit the Paper Orientation property.
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperOrientation', 'landscape');
set(gcf, 'PaperPosition', [.25 .25 10.5 8]);
If you are exporting the figure for other purposes, you will want to remove the border from PaperPosition. See Ben Hinkle's Exporting Figures for Publication for more details (and exportfig.m).

Changing plot color and linestyles. If you are tired of the standard plot colors and linestyles, and don't really want to specify them for every line, you can edit DefaultAxesColorOrder and DefaultAxesLineStyleOrder. Matlab cycles through all colors before moving to the next linestyle. This seems to be unchangeable. Colors are specify in a 3 by n matrix, where n is the number of colors. Linestyles are specified in a string using the standard linestyle characters, separated by "|", e.g., "-|--|:". As an example,
set(gcf,'DefaultAxesColorOrder',[0 0 0 ; 0 .1 .9]) set(gcf,'DefaultAxesLineStyleOrder','-|:')
cycles through black and dark blue solid then black and dark blue dotted.

Annotating the plot. From time to time, you may want to write data or comments on to the plot, or perhaps next to the plot. I used to try to cram information into xlabel, ylabel and title. No more. The appropriate command is annotation, which itself is quite rich. I will be illustrating the textbox annotation.

An issue with textbox annotation is that coordinates are specified with respect to percentages of the figure. To make the placement neat, leverage subplots. I wanted to make a page with four rows of two plots each with a box to the right detailing, well, the details. Here's how I did it:

% setup first plot %
% setup second plot %
What's going on. After switching to the target subplot, I used get(gca,'Position') to fetch the dimensions of the subplot in percentages of the figure. This way I don't have to guess where the textbox should be. LineStyle is set to 'none' to eliminate the default border. For formatting, I found the section on the String to be quite helpful. To enter a multiline string, separate the lines using cells, e.g.,
mystring = {sprintf('[%s]',EEG.tu.label)...
  ,sprintf('wC4-dT6: %.1f%%',100*dryrate)...
  ,sprintf('wC4-wT6: %.1f%%',100*wetrate)...
  ,sprintf('nTest = %d',EEG.chanlocs(1).nTest)}

Posted by torque at March 11, 2005 4:49 PM | TrackBack
Post a comment

Remember personal info?