How to avoid marker clipping while saving two axis figure to pdf?

Hi everyone,
I have some trouble while exporting a two axis figure to pdf. The following example code works great while creating the figure in Matlab. But in the generated pdf-file all my red markers on the x-axis get cut off (see screenshot). Curiously this problem only occures, when I use a two axis plot
fig = figure;
x = linspace(0,25);
y = sin(x/2);
marker = linspace(0,25,5);
yyaxis left
plot(x,y,'g');
hold on;
r = x.^2/2;
yyaxis right
plot(x,r,'b');
hold on;
plot(marker,0,'rx','Markersize', 8);
saveas(gcf,'myfigure.pdf');
Can anyone please help me out here?

 Accepted Answer

Not the first time I see clipping issues with plotyy. Simple solution, avoid using plotyy
% Create two axes
ax(1)=axes('color','none');hold on
ax(2)=axes('yaxislocation','right','xcolor','none','color','none');hold on
x = linspace(0,25);
y = sin(x/2);
marker = linspace(0,25,5);
% Toggle left
axes(ax(1))
plot(x,y,'g');
hold on;
r = x.^2/2;
% Toggle right
axes(ax(2))
plot(x,r,'b');
hold on;
plot(marker,0,'rx','Markersize', 8);
% Link axes, IMPORTANT!
linkaxes(ax,'x')
% Save
saveas(gcf,'myfigure.pdf');
Possibly, the reason for the yyaxis annoying clipping is that the default clippingstyle is set to 'rectangle' instead of the normal '3dbox'. Changing the clippingstyle is however difficult, as it keeps reverting back to 'rectangle' when toggling yyaxis.

More Answers (2)

Hi Jonas,
thanks a lot!!! :) That is working just fine. I've just got some other issue now with the right y-axis, because now I have some small black lines coming from the other side axis which are disturbing the scale. Do you know how to remove them?

1 Comment

Im on mobile so cannot verify right now but I suspect a

set(ax,'box','off')

will do that for you. You may also want to either toggle the grid off or align the ticks so that you get a single grid working for both yaxes.

Sign in to comment.

Many thanks Jonas. You've helped me a lot here. Unfortunately I'm still struggling with the figure, because I need to put it in a special paper size. When I do so, the figure breaks completely. I think it`s because I´m inserting a label to the x-axis. Here is my code:
figname='myfigure.pdf';
myfigure=figure('PaperSize',[4 2.5]*2.7,'PaperUnits','Inches');
set(gcf,'Units','Pixels','Position',[300 50 400 250]);
set(gcf,'Color',[1,1,1]);
% Create two axes
ax(1)=axes('color','none');hold on
ax(2)=axes('yaxislocation','right','xcolor','none','color','none','ycolor','red');hold on
x = linspace(0,25);
y = sin(x/2);
r = x.^2/2;
marker = linspace(0,25,5);
% Toggle left
axes(ax(1));
plot(x,y,'g');
hold on;
grid on;
box on;
XLabel = xlabel('Time','Interpreter','Latex');
Y1label = ylabel('Y1 Label','Interpreter','Latex');
hold on;
% Toggle right
axes(ax(2));
plot(x,r,'b');
hold on;
Y2label = ylabel('Y2 Label','Interpreter','Latex');
hold on;
plot(marker,0,'rx','Markersize', 8);
% Link axes, IMPORTANT!
linkaxes(ax,'x')
set(gca,'FontSize',9,'FontAngle','normal');
set(XLabel,'FontSize',11,'FontAngle','normal');
set(Y1label,'FontSize',11,'FontAngle','normal');
set(Y2label,'FontSize',11,'FontAngle','normal');
set(gcf,'PaperPositionMode','auto');
% Save
print(myfigure, '-dpdf', figname);
Do you have any advice what I can do to fix this?

2 Comments

Yes, the labelling changes the axes position of only one axes. Add this at the end of the script before you print.

ax(2).Position=ax(1).Position

As a sidenote, please use the "comment" instead of "answer" for anything other than actual answers :)

Perfect, that is working very well for me. Thanks a lot Jonas ;-)

Sign in to comment.

Categories

Products

Release

R2018a

Asked:

on 13 Sep 2018

Commented:

on 17 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!