Axis Clipping not working in pdf print
3 views (last 30 days)
Show older comments
I have a problem with Matlab 2018a when exporting a plot to pdf. The code im using is the following:
if true
% code
F1 = figure(1);
set(gcf,'Units','centimeter');
set(gcf,'Position',[10 10 f_width f_height]);
F1.Color = [1 1 1];
set(F1,'defaultAxesColorOrder',[[0 0 0] ; [0 0 0]]);
% Plot
H1 = plot(t,Vac1a,'Color',[0 0 1]);
grid on;
hold on;
H2 = plot(t,Vac2a,'Color',[0 128/255 0]);
yyaxis right
H3 = plot(t,iLa,'Color',[1 0 0]);
% Manipulate Axis
AX1 = gca;
xlim(AX1,[10e-3 10e-3+60e-6]);
set(AX1,'Layer','Top');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%set(AX1,'Units','centimeters');
% Save to png and pdf
print(gcf, '-dpng','-r600', ['.\Data\png\',outputname,'.png']);
set(gcf,'PaperSize',[f_width f_height]);
set(gcf,'PaperPosition',[0 0 f_width f_height]);
set(gcf,'Renderer','painters');
print(gcf,'-dpdf', ['.\Data\pdf\',outputname,'.pdf']);
end
When I open the resulting files the png looks exactly like the figure I get in the box (which is what I want). In the pdf however, the plot lines inside the box are exceeding the box on the right side (see attached pdf). How do I get rid of this? The vectors I plot are way longer than what I want to plot, but since I'm setting the x-limits this shouldn't be a problem (an never was so far in previous files)?
0 Comments
Answers (1)
jonas
on 18 Aug 2018
Edited: jonas
on 18 Aug 2018
I think I have finally figured it out. The issue is related to the yyaxis right command, which creates a 2nd yaxis with default 'ClippingStyle' set to 'rectangle' instead of the usual '3dbox'. You would think that the easiest fix would be to change the ClippingStyle. Wrong. When you plot, the axes somehow updates and the ClippingStyle reverts back to its default 'rectangle'. I've encountered similar issues previously a few times (see e.g. my only question on this forum) without finding a solution.
Fortunately, I have a solution for you. Create two axes manually instead of yyaxis. Here's an example:
%%Some data
x=[0 2];
y=[-.5 .5];
%%Make figure and first axes
figure;
ax1=axes; hold on
plot(x,y,'b')
%%Make second axes
ax2=axes;
plot(x,y+0.1,'r')
%%Link their x-axes and set some limits
linkaxes([ax1,ax2],'x')
set(ax2,'yaxislocation','right')
set([ax1 ax2],'ylim',[-1 1],'xlim',[.5 1],'color','none')
print(gcf,'-dpdf','test.pdf')
The resulting figure is perfectly clipped. When I make the same plot using yyaxis, the lines extend beyond the right yaxis, just like in your case.
0 Comments
See Also
Categories
Find more on Annotations 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!