MATLAB figures that advertise rather than antagonize

There are no excuses for poor figures in a published scientific article. When I say “poor” I mean any of the following:

  1. The figure is so small my eyes strain to read it.
  2. The axes are unlabeled, or are given some label that does not make sense with the units, e.g., “Time” with an right-hand abscissa of 10×5.
  3. The text in the figure is smaller than the text in the caption, requiring me to find a magnifying glass to read it.
  4. The figure caption is vague and uninformative.
  5. The figure becomes unclear in grayscale because to me the colored lines all look the same.

An article should function as an advertisement of research, as reflected in the working methodology of reproducible research. And as with any advertisement, it should not be annoying. But I have learned that many people are not aware of how easy it is to automatically create readable figures in MATLAB. As an example, I created the following code to show how. Note that there is no need to invoke the figure editor of MATLAB and manually adjust and change all the details. Creating such code will make it much easier to share with other scientists (another tenet of reproducable research), and make quick adjustments when necessary. And I am in a happier mood when I read a paper with figures that advertise rather than antagonize.

% should always do this first to get rid of possibly interfering data
clear all;
% create a new figure and store handle
screenposx = 300;   % in units of pixels
screenposy = 300;
screenwidth = 600;
screenheight = 400;
% The normalized units setting means we do not need to worry about pixels or inches when creating an axis
handle_figure = figure('Position',[screenposx screenposy screenwidth screenheight], 'Units','Normalized');
% set the figure printing properties.
% NOTE: if printing to EPS, only the last 2 values matter as they define the figure size
leftmargin = 0.1;  % there are in inches
bottommargin = 0.1;
figurewidth = 6;
figureheight = 4;
set(handle_figure,'PaperOrientation','portrait', 'PaperPosition',[leftmargin bottommargin figurewidth figureheight]);
% create axes and store handle
axeswidth = 0.86; % normalized units; 1 spans entire width
axesheight = 0.86;
axesmarginleft = 0.12;
axesmarginbottom = 0.13;
% always make the fontsize larger than the extremely small MATLAB default
handle_axes = axes('FontSize',14, 'FontName','Helvetica', 'position', [axesmarginleft axesmarginbottom axeswidth axesheight]);
t = [-pi:0.01:pi];
x = sin(t);
y = cos(t);
% plot data and store handles
handle_sin = plot(t,x);
hold on;
handle_cos = plot(t,y);
% add the xy axis
line_x = line([-pi pi],[0 0]);
line_y = line([0 0],[-2 2]);
% now we set plot properties so they look nice. Note: [0 0 0] = black, [1 1 1] = white
set(handle_sin,'LineWidth',1,'Color',0*ones(3,1));
set(handle_cos,'LineWidth',2,'Color',0.6*ones(3,1));
set(line_x,'Color','k','LineStyle','--');
set(line_y,'Color','k','LineStyle','--');
% Note how I set the black line to a small width, and the gray line to a thicker width.
% set axis properties by adding nice labels and units
set(handle_axes,'XTick',[-pi:pi/4:pi], 'YTick', -1:0.25:1, 'XTickLabel',{'-pi', '-3pi/4', '-pi/2', '-pi/4', '0','pi/4','pi/2','3pi/4','pi'},'YTIckLabel', {'-1','-3/4','-1/2','-1/4','0', '1/4','1/2','3/4','1'});
% note that we can use Latex-like symbols
xlabel('angle \omega');
ylabel('f(\omega)');
axis([-pi pi -1.05 1.05]);
grid on;
% create a legend
handle_legend = legend([handle_sin handle_cos],'Sine','Cos');
% reposition legend so that it does no interfere
% first we get the position property of the legend to retain its size
handle_pos = get(handle_legend,'Position');
% position the legend at a different location, keeping its original width and height
set(handle_legend,'Position',[0.6 0.2 handle_pos(3) handle_pos(4)]);
% finally, print a nice EPS
print(handle_figure,'-depsc2','sine.eps');

Et voilá! (Based on your version of MATLAB, you might have to adjust the fontsize and the margins.)
sine.png
Now what would be extremely nice is a way to use LaTeX to turn those “pi/2” into \(\pi/2\) without having to use the “text” command.

Reblog this post [with Zemanta]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s