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 件のコメント

Adam Danz
Adam Danz 2024 年 4 月 26 日
編集済み: Adam Danz 2024 年 4 月 26 日
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
Jamie Bragg 2024 年 4 月 26 日
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.
Sam Chak
Sam Chak 2024 年 4 月 26 日
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'.
Adam Danz
Adam Danz 2024 年 4 月 26 日
編集済み: Adam Danz 2024 年 4 月 26 日
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.
Paul
Paul 2024 年 4 月 26 日
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
Sam Chak 2024 年 4 月 26 日
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
Jamie Bragg 2024 年 4 月 29 日
@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.
Adam Danz
Adam Danz 2024 年 5 月 2 日
@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!

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2024 年 5 月 6 日
編集済み: Adam Danz 2024 年 5 月 6 日

2 投票

This appears to be a bug specific to the OpenGL renderer. Thanks for reporting this issue. Investigation will continue internally.

7 件のコメント

Sam Chak
Sam Chak 2024 年 5 月 6 日
Thank you, @Adam Danz.
Ed Wheatcroft
Ed Wheatcroft 2025 年 2 月 27 日
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')
Adam Danz
Adam Danz 2025 年 2 月 27 日
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.
Ed Wheatcroft
Ed Wheatcroft 2025 年 2 月 27 日
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().
Adam Danz
Adam Danz 2025 年 2 月 27 日
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.
Walter Roberson
Walter Roberson 2025 年 2 月 27 日
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 ;-)
Adam Danz
Adam Danz 2025 年 2 月 27 日
That expectation is a safe one.

サインインしてコメントする。

その他の回答 (1 件)

Sam Chak
Sam Chak 2024 年 4 月 26 日

0 投票

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 件のコメント

Jamie Bragg
Jamie Bragg 2024 年 4 月 26 日
I tried running your code and I still have the same issue as before. The image you showed is the saved png file right?
Sam Chak
Sam Chak 2024 年 4 月 26 日
Nope, it was generated by MATLAB online. The saveas() command has issue. Try this one instead:
print('myPlot', '-dpng')
Jamie Bragg
Jamie Bragg 2024 年 4 月 26 日
Tried using the print() command, and I have the same issue.
Sam Chak
Sam Chak 2024 年 4 月 26 日
@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
Jamie Bragg 2024 年 4 月 26 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

リリース

R2024a

質問済み:

2024 年 4 月 26 日

コメント済み:

2025 年 2 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by