Why does my plot line appear outside the axes?

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])
The image I sent is the png exported image. It looks fine in MATLAB. (I tried exporting as a tiff and it looks the same as the png.)
Here's the fig file.
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.
@Jamie Bragg, @Sam Chak, and anyone else who sees this bug:
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'));
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
>>
@Adam Danz I was originally using MATLAB desktop, which is where the bug occurs. When I run the code you suggest, I get:
r =
struct with fields:
GraphicsRenderer: 'OpenGL Hardware'
Vendor: 'Intel'
Version: '4.5.0 - Build 31.0.101.4953'
RendererDevice: 'Intel(R) Iris(R) Xe Graphics'
Details: [1×1 struct]
ans =
struct with fields:
RendererDriverVersion: '31.0.101.4953'
RendererDriverReleaseDate: '2023-11-7'
HardwareSupportLevel: 'Full'
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
SupportsGraphicsSmoothing: 1
MaxTextureSize: 16384
MaxFrameBufferSize: 16384
When using MATLAB online (Firefox), the saved PNG image doesn't have the issue. I get the same information as Sam Chak when running the commands you suggest.
@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!

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 6 May 2024
Edited: Adam Danz on 6 May 2024
This appears to be a bug specific to the OpenGL renderer. Thanks for reporting this issue. Investigation will continue internally.

7 Comments

I just ran into a very similar issue when trying to save a figure as a .eps file (see example). The bug seemed wierdly sensitive to the value of the y data being plotted on the right hand axis: If I un-comment the cos(3x) line below then the issue disappears. I also had no problems when I ran the OP's example.
clear
close all
clc
% set up data
x = 0:0.1:10;
yl = sin(x);
yr = 0*x;
m = x > 1 & x <3;
yr(m) = 1;
% yr = cos(3*x); % The issue disappears if this is used as the RH y data.
% plot LH data
plot(x, yl)
% plot RH data
yyaxis right
plot(x, yr)
% switch back to LH axis and set limits
yyaxis left
xlim([0 5])
% save as .eps
saveas(gca,'test.eps')
Hi Ed, this is unrelated to the bug described by OP. I assume the unexpeted results are the orange line on the top and bottom of the axes, is that correct? I'm not sure how layering is handled during export to or import from eps files.
Try setting the axes later to top so that the black axes line is on top of the step function line,
ax = gca;
ax.Layer = "top";
Another thing to try is adding some padding to the axes limits either by directly setting xlim and ylim or by using axis padded.
Hi Adam, the issue is not with the figure shown in the GUI (and also by the online compiler), but with the file which is produced by the save to eps. When I do this, the data on the RH y-axis appears outside the plot area, similar to the OP's issue when saving to PNG - See screengrab below (with ylims adjusted for clarity).
I should also note that I made this by manually clickling through file>save as from the GUI, but I get the same result from the call to saveas().
Thanks Ed, now the issue is clear.
Please contact tech support and include instructions how to reproduce the problem along with your release information and the output to rendererinfo(ax) where ax is the axes handle to your yyaxis.
This appears to be a bug specific to the OpenGL renderer.
Ah, then it would be expected to be fixed in R2025a, which no longer uses OpenGL ;-)
That expectation is a safe one.

Sign in to comment.

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

I tried running your code and I still have the same issue as before. The image you showed is the saved png file right?
Nope, it was generated by MATLAB online. The saveas() command has issue. Try this one instead:
print('myPlot', '-dpng')
Tried using the print() command, and I have the same issue.
@Jamie Bragg, If the image is displayed correctly in MATLAB, what happens when you save the figure again after it has been properly rendered?
I saved the figure manually from the figure MATLAB figure window after it rendered, and looks fine!
But if I run the command to save it after it's rendered, it still has the clipping issue.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2024a

Asked:

on 26 Apr 2024

Commented:

on 27 Feb 2025

Community Treasure Hunt

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

Start Hunting!