How to make a really beautiful and publishable graph with matlab is not easy but possible. It is very good descriped in this article making pretty graphs.

However once you want to export your file you run into many problems, which are build in bugs of matlab, and have not been fixed since years. They are however well documented by matlab…
The two most important bugs are the missing font support and wrong linestyles. If you use any not standard postscript font such as “Times, Helvetica, Courier, Bookman” then the font gets replaced by Courier and your graph looks really ugly. The linestyles can only be fixed in the eps file directly, not in any other output format. Additionaly matlab suffers from the legend box size bug, which most often is too small in width.

Matlab offers three renders. Most important are ‘painters’ which is the only vector renderer. It should be used for vector suitable data such as lines. The other is ‘opengl’ which is used by default for 3D data such as ‘surf’ plots.

In the following I demonstrate these problems and also show the results of macros providing better results. These are export_fig, savefig, plot2svg and matlabfrag. In the end I show the results of my own approach currently named ‘latexfigure’. It reuses many code and ideas of the other matlab projects.

[TOC]

figure code

clf; clc; clear all;
 
figure_width  = 8*2;
figure_height = 6*2;
FontSize = 11*1.5;
FontName = 'MyriadPro-Regular';
 
xaxis = linspace(0,10,50);
data1 =sin(xaxis);
data2 =sin(xaxis + 0.6);
data3 =sin(xaxis + 1.2);
 
hfig  = figure(1); clf;
    set(gcf, 'units', 'centimeters', 'pos', [0 0 figure_width figure_height])
    % set(gcf, 'Units', 'pixels', 'Position', [100 100 500 375]);
    set(gcf, 'PaperPositionMode', 'auto');
    set(gcf, 'Color', [1 1 1]); % Sets figure background
    set(gca, 'Color', [1 1 1]); % Sets axes background
    set(gcf, 'Renderer', 'painters'); 
 
hLine1 = line(xaxis, data1);
hLine2 = line(xaxis, data2);
hLine3 = line(xaxis, data3);
hdots = line(xaxis, data3);
 
 
set(hLine1                        , ...
  'LineStyle'       , '--'        , ...
  'LineWidth'       , 2           , ... 
  'Color'           , [0.75 0 0]  );
 
set(hLine2                        , ...
  'LineStyle'       , ':'         , ...
  'LineWidth'       , 2           , ...   
  'Color'           , [0 0 0.75]  );
 
set(hLine3                        , ...
  'LineStyle'       , '-'         , ...
  'LineWidth'       , 2           , ...   
  'Color'           , [0.5 0 0.5]  );
 
set(hdots                         , ...
  'LineStyle'       , 'none'      , ...
  'Marker'          , 'o'         , ...
  'MarkerSize'      , 6           , ...
  'MarkerEdgeColor' , [.5 .0 .5]  , ...
  'MarkerFaceColor' , [.7 .5 .7]  );
 
 
hTitle  = title ('test graphics');
hXLabel = xlabel('x-axis');
hYLabel = ylabel('y-axis');
 
hLegend = legend( ...
  [hdots, hLine1, hLine2, hLine3], ...
  'Data' , ...
  'Model'    , ...  
  'Fit'      , ...
  'Validation Data'       , ...  
  'location', 'Best' );
 
  %'Data (\mu \pm \sigma)' , ...
  %'Model (\it{C x^3})'    , ...  
  %'Fit (\it{C x^3})'      , ...
 
set( gca                       , ...
    'FontName'   , FontName );
set([hTitle, hXLabel, hYLabel], ...
    'FontName'   , FontName);
set([hLegend, gca]             , ...
    'FontSize'   , FontSize - 2);
set([hXLabel, hYLabel]  , ...
    'FontSize'   , FontSize    );
set( hTitle                    , ...
    'FontSize'   , FontSize    , ...
    'FontWeight' , 'bold'      );
 
set(gca, ...
  'Box'         , 'off'     , ...
  'TickDir'     , 'out'     , ...
  'TickLength'  , [.02 .02] , ...
  'XMinorTick'  , 'on'      , ...
  'YMinorTick'  , 'on'      , ...
  'YGrid'       , 'on'      , ...
  'XColor'      , [.0 .0 .0], ...
  'YColor'      , [.0 .0 .0], ...
  'YTick'       , -1:0.5:1, ...
  'LineWidth'   , 1.0         );
 
 
% print(gcf, '-r300', 'matlab_opengl.png', '-dpng');
% print(gcf, '-r300', 'matlab_opengl.pdf', '-dpdf');
% print(gcf, '-r300', 'matlab_opengl.eps', '-depsc');
% ghostscriptConvertTo('png', 'matlab_opengl.eps', 'matlab_opengl.eps.png')
 
% print(gcf, '-r300', 'matlab_painters.png', '-dpng');
% print(gcf, '-r300', 'matlab_painters.pdf', '-dpdf');
% print(gcf, '-r300', 'matlab_painters.eps', '-depsc');
% ghostscriptConvertTo('png', 'matlab_painters.eps', 'matlab_painters.eps.png')
% 
% savefig('savefig', 'pdf', 'png', 'eps', '-fonts'); 
% 
% export_fig('exportfig_painters.pdf', '-pdf', '-painters');
% export_fig('exportfig_painters.eps', '-eps', '-painters');
% export_fig('exportfig_painters.png', '-png', '-painters', '-r300');
% 
% export_fig('exportfig_opengl.png', '-png', '-opengl', '-r300');
% 
% plot2svg('plot2svg.svg', hfig);
 
% matlabfrag('matlabfrag.eps','handle',hfig) ;
% mlf2pdf(gcf,'matlabfrag');
 
% latexPackages = ['\renewcommand{\sfdefault}{pmy}\n \renewcommand{\rmdefault}{pmy}'];
% latexfigure(hfig,'latexfigure','eps',latexPackages);
% latexfigure(hfig,'latexfigure','png',latexPackages);
% latexfigure(hfig,'latexfigure','pdf',latexPackages);