Spikes when saving matlab figure as pdf
2 views (last 30 days)
Show older comments
Hello
When I plot my figure to a pdf-file, then I get spikes on the figures in the pdf-file which are not present in the original matlab figure. Does anyone know how to get rid of these spikes?
Below you can see the original matlab figure followed by the pdf-file. The spikes are best visible in the right top.
For saving the figure as pdf-file I used the code below:
h=gcf;
set(h,'PaperOrientation','landscape');
set(h,'PaperPosition', [0 0 plotheight plotwidth]);
print(gcf,'-dpdf',title));
2 Comments
Benjamin Kraus
on 13 Feb 2018
Edited: Benjamin Kraus
on 13 Feb 2018
That seems strange. My guess is that the difference has to do with the resolution of your screen compared to the resolution of the file you are outputting.
How are you generating the plots on the right? Are you using bar or stem or plot (or something else)? From the look of the graph, I'm going to guess you are using bar, but you probably should be using stem.
Any chance you can provide sample data and code that reproduces the top-right graph?
If you manually inspect the data, which plot is correct? Are the spikes missing from the top graph, or are they erroneous spikes in the bottom graph?
Answers (1)
Ted Shultz
on 2 Nov 2018
This is a MATLAB bug, and has been happening for a few years. It drives me crazy. For some discussion on this, and a workaround, see: https://github.com/altmany/export_fig/issues/6.
More or less, you save your figure as an eps (print -epsc2 filename.ep) and then run this function on it to fix it:
function fixeps(inname,outname,fixmode)
if nargin==2
fixmode = 'LJ';
end
fi = fopen(inname,'r');
fo = fopen(outname,'w');
tline = fgets(fi);
while ischar(tline)
if (strcmp(tline,['10.0 ML' 10])) % Replace 10.0 miterlimit
switch (fixmode)
case 'LJ'
fwrite(fo,['1 LJ' 10]); % With round linejoin
case 'ML'
fwrite(fo,['2.5 ML' 10]); % With smaller miterlimit
end
else
fwrite(fo,tline);
end
tline = fgets(fi);
end
fclose(fo);
fclose(fi);
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!