nav-left cat-right
cat-right
Matthias Pospiech > Programming > matlab > Matlab figures – solving export problems

Matlab figures – solving export problems

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.

  1. figure code

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);

Seiten: 1 2 3 4

15 Antworten : “Matlab figures – solving export problems”

  1. pospiech sagt:

    I do not have a solution for you, sorry. Note that if you want to have a wider audience you should ask matlab related questions on stackoverflow.com. The link to my plugin on matlabcentral is https://www.mathworks.de/matlabcentral/fileexchange/41894-latexfigure.

  2. Jan sagt:

    Hi there. I have used the script excessively and it has produced a lot of nice looking figures. But is there a way to narrow down the legend?

    I define a size for the legend using a four element vector for the setting of the ‘location’ but MATLAB is widening the legend so that the entry for the legend fits. When I use for the example of a revolution speed
    $n = 500/\textrm{min}$
    I get a very wide legend with a big white edge after latexfigure converts the term to n = 500/min.

  3. Jan sagt:

    Thanks, I added my issue to the existing issue to the page. I am running into the problem using your latexfigure.m function. It’s not a problem using Palatino. It’s a general problem, that I only get the Charter font no matter which FontName I define.

  4. pospiech sagt:

    You are not making it really clear what you are doing. If you are using the print command of matlab you should ask your question on stackoverflow.com. If you are using my latexfigure script then I do not understand where the problem is. Note also that there is a special page for reporting issues (not questions): code.google.com/p/latexfigure/.

  5. Jan sagt:

    Hi there, Jan again (from the third to last post). I ran into a little problem using Palatino for my figures. I tried setting the font name to ‘Palatino’ and ‘Palatino Linotype’ and MATLAB is setting the correct font in the figure but when I convert to eps or pdf the look is similar but not the same. This is very obvious when using the letter R, which is getting a little swing in the bottom right corner. I also checked the mathpazo font of LaTeX but that one has the same look as the Palatino Linotype of Windows although it is a slightly different clone of the original.

    Is there a solution to this problem or a hint where to look at?

  6. Jan sagt:

    Hey! Nice Tool so far, I was looking for something like that since “normal” export via export_fig or print does mess up my unicode characters (I am working on Ubuntu Linux @ UTF-8).
    For Latex matlabfrag is the best choice I think, but i need a vectorized graphic with the true annotations, not just labels.

    But I realized the created fonts are no text anymore, is that correct? Is there no way back from the *.tex and the *.eps file with labels to the initial eps file (with the “real” text)?

    Btw: For UTF-8 System you have to change line 165 to fprintf(fid,’\\usepackage[utf8]{inputenc}\n’);

    Thank you!

    Best regards,
    Jan

  7. pospiech sagt:

    You should try to run ‘latex’ from a console. If that fails you should add it to your path. If you do not know how to do that ask it somewhere else.
    If it fails even with a set up path you can specify a path with the option ‘distpath’.

  8. Jan sagt:

    Hello,
    thanks for the tool. I’m trying to get around my font-problem using “Palatino Linotype” for my graphics. But I’m already running into problems using the example in “plotTest.m”

    Error using latexfigure/runLaTeX (line 204)
    Error 1 — LATEX:
    Das System kann den angegebenen Pfad nicht finden.

    Error in latexfigure (line 101)
    success = runLaTeX();

    Error in plotTest (line 91)
    latexfigure(hfig,’latexfigure’,'eps’,'packages’, latexPackages);

    It seem’s that I need to define the path of my LaTeX-Distribution first. How do i do that?

  9. Andreas Hielscher sagt:

    Indeed, the gs converter has to be blamed – exporting the pdf via Adobe Acrobat Professional works perfectly. Thanks for your help!

  10. pospiech sagt:

    The resulting pdf is generated by LaTeX. Therefore you should find out if you really load a vector (type 1) font. The eps is generated from the pdf file using ghostscript. Please check if you also experience this problem with pdf output. If not I would blame the gs converter.

  11. Andreas Hielscher sagt:

    That is a really nice tool and it helped me to get around the “FontName”-Bug in Matlab. Thanks a lot!

    I do have one problem: After using latexfigure, my eps-file looks fine, but when I zoom in, the font is looking like a bitmap and not smooth like a vector. I tried several fonts, eg Cambria (my favourite one), a latex-font within the package kpfonts and a matlab-font Palatino. All fonts look like a bitmap.
    What should I change?

    Thank you in advance and best regards,
    Andreas Hielscher

  12. Jannes sagt:

    Thanks for the great code!

    I do have a problem when trying to plot over very small or large ranges (i.e. 1E-07, 1E07..). It seems that Ghostscript can’t handle that form of notation? I’m running Matlab on OSX (Snow Leopard).

    Thanks a lot

  13. pospiech sagt:

    Currently I have not the time for this code (I am partly still working on it). But I promise to publish it within the next month…

    In the meantime you can still use the matlabfrag package which is what I use internally.

  14. Oliver Woodford sagt:

    The output of latexfigure looks great! I’ve wanted to find a good solution to separating text and graphics for some time, and this looks like it. I would love to see it on the Matlab Central File Exchange, or else be able to download the latest version from somewhere.

Einen Kommentar schreiben

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *


*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP-SpamFree by Pole Position Marketing