Search code examples
matlabpdfprintingploteps

Matlab print -dpdf produces fuzzy lines


I have a problem when printing a 2D plot to pdf in matlab. I'm trying to print the following plot to a file (pdf, eps, svg, doesn't matter): Original plot

The problem is that at some points the line is very "jiggly" (sorry for the lack of a better word). I zoomed in on the upper part so you can see what I mean: Zoomed in in matlab figure window

This is obviously not a problem for the matlab figure window. But when I print it to pdf, this is what it looks like:

Resulting PDF

The result is identical for pdf, svg and eps. I guess the problem is that matlab is creating a vectorized path (that is good!) but the path line is too thick and then every little spike can be seen.

Here's the code I'm using to produce the pdf:

sTitle = 'trajectory';
sFile = 'Data/trajectory.mat';
sPdfFile = 'pdfs/trajectory.pdf';
linewidth = 1;
fontsize1 = 18;

fig = figure;

% Adjust figure window size
set(fig, 'Position', [100 100 1400 800]);

% Set title
title(sTitle);

% Get states
[s, t] = load_data(some_data);

% Draw trajectory
plot(s(1,:), s(2,:), 'linewidth', linewidth);

% Labels and stuff
xlabel('x^W [m]', 'fontsize', fontsize1);
ylabel('y^W [m]', 'fontsize', fontsize1);
set(gca, 'fontsize', fontsize1)

% Axis font
set( gca                       , ...
    'FontName'   , 'Helvetica' );

set(gca, ...
  'Box'         , 'on'     , ...
  'TickDir'     , 'out'     , ...
  'TickLength'  , [.02 .02] , ...
  'XMinorTick'  , 'on'      , ...
  'XGrid'       , 'on'      , ...
  'XMinorGrid'  , 'off'     , ...
  'YMinorTick'  , 'on'      , ...
  'YGrid'       , 'on'      , ...
  'XColor'      , [.3 .3 .3], ...
  'YColor'      , [.3 .3 .3], ...
  'XTick'       , -5:1:5, ...
  'XTickLabelMode', 'auto', ...
  'YTick'       , -5:1:5, ...
  'LineWidth'   , 1         );

% Adjust view
axis([-2.5 2.5, -2.7 0.5]);

% Correct data aspect ratio
daspect([1,1,1])


% Print to PDF
width = 10;
height = 5;
set(gcf, 'PaperPosition', [0 0 width height]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [width height]); %Set the paper to have width 5 and height 5.
print('-dpdf', '-r600', sPdfFile);

Solution

  • According to this answer, this is an acknowledged bug, and the answerer provided a function to correct the issue for EPS files.

    Since you are creating a PDF, I'd suggest using export_fig (requires a Ghostscript install) which, on the test script below, creates a smooth line in the produced PDF.

    clc();
    clear();
    
    figure(1);
    
    % Get states
    n  = 800;
    x = linspace(0,2*pi,n);
    s = [x.*cos(x);x.*sin(x)] + 0.3*exp(-0.3*[x;x]).*(rand(2,n)-0.5) ;
    
    % Draw trajectory
    plot(s(1,:), s(2,:), 'linewidth', 1);
    axis([-20,20,-20,20]);
    daspect([1,1,1])
    
    % Print to PDF
    print('traj.pdf','-dpdf', '-r600');
    export_fig('traj2.pdf','-dpdf','-r600');
    

    print PDF output:

    [print PDF output image

    export_fig PDF output:

    export_fig PDF output image