Why does my plot line appear outside the axes?
Show older comments
I'm trying to make a double y-axis plot of data that extends beyond the x-axis limits of the plot. When I save the plot as a .png, the plot lines appear outside the right axis of the plot.
Here's code for a simplified version of what I'm trying that produces the issue:
x = [0 1 2 3 4];
yl = [0 1 2 4 3];
yr = [0 2 3 1 2];
yyaxis left
plot(x, yl)
yyaxis right
plot(x, yr)
xlim([1 3])
saveas(gcf,'[PATH].png')
And here's the image that's saved:

I know that there's a workaround of shortening the data arrays to the range of the x-axis limits before plotting, but I was wondering if there's a more elegant solution.
8 Comments
I could not reproduce this in MATLAB online. See Star Strider's answer. If that doesn't fix it, please save the figure as a fig file and attach it here.
I'm puzzled because the image in your question has gaps in the lines where the right y axis ticks are. Something's fishy here. @Jamie Bragg, is this image what appears in MATLAB or does it only appear in the png exported image?
Here's the plot your code produces using the Run feature (currently using R2024a)
x = [0 1 2 3 4];
yl = [0 1 2 4 3];
yr = [0 2 3 1 2];
yyaxis left
plot(x, yl)
yyaxis right
plot(x, yr)
xlim([1 3])
Jamie Bragg
on 26 Apr 2024
Sam Chak
on 26 Apr 2024
Actually, when I ran this in MATLAB online, it briefly displayed the extension of the lines before automatically correcting itself and saved the expected result as 'test.png'.

This appears to be a bug but I cannot reproduce it in MATLAB Online on my end.
Could you share what this returns in MATLAB online using a figure that displays the problem? Feel free to reply here or, if you'd prefer, via email by using the contact button in my profile.
ax = gca();
r = rendererinfo(ax)
r.Details
Also, please let me know what browser you're using.
Seems to run o.k. here.
x = [0 1 2 3 4];
yl = [0 1 2 4 3];
yr = [0 2 3 1 2];
figure
yyaxis left
plot(x, yl)
yyaxis right
plot(x, yr)
xlim([1 3])
saveas(gcf,'temp.png')
figure
imshow(imread('temp.png'));
Sam Chak
on 26 Apr 2024
Hi @Adam Danz
I'm using Google Chrome. I re-ran the code in MATLAB Online and added the rendererinfo() command. It briefly displayed the extension of the lines (image 1) before automatically correcting itself (image 2). I also copied the output displayed in the Command Window and pasted it below. The PNG image is also attached.
Thank you for looking into this matter.
Image #1:

Image #2:

Output displayed in the Command Window:
>> untitled
r =
struct with fields:
GraphicsRenderer: 'OpenGL Software'
Vendor: 'Brian Paul'
Version: '2.1 Mesa 17.1.3'
RendererDevice: 'Mesa X11'
Details: [1x1 struct]
ans =
struct with fields:
HardwareSupportLevel: 'None'
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
SupportsGraphicsSmoothing: 0
MaxTextureSize: 16384
MaxFrameBufferSize: 16384
>>
Jamie Bragg
on 29 Apr 2024
Adam Danz
on 2 May 2024
@Sam Chak and @Jamie Bragg thanks for sharing this renderer information. It shows exactly what I needed to see. I'll take it from here and add this bug to our tracker. Thanks again for your help and cooperation!
Accepted Answer
More Answers (1)
Because the xlim() command is used on the right y-axis plot.
x = [0 1 2 3 4];
yl = [0 1 2 4 3];
yr = [0 2 3 1 2];
yyaxis left
plot(x, yl), xlim([1 3])
yyaxis right
plot(x, yr), xlim([1 3])
% saveas(gcf,'[PATH].png')
5 Comments
Jamie Bragg
on 26 Apr 2024
Sam Chak
on 26 Apr 2024
Nope, it was generated by MATLAB online. The saveas() command has issue. Try this one instead:
print('myPlot', '-dpng')
Jamie Bragg
on 26 Apr 2024
Sam Chak
on 26 Apr 2024
@Jamie Bragg, If the image is displayed correctly in MATLAB, what happens when you save the figure again after it has been properly rendered?
Jamie Bragg
on 26 Apr 2024
Categories
Find more on Graphics Performance 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!




