Why does my plot line appear outside the axes?
53 次查看(过去 30 天)
显示 更早的评论
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
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
2024-5-6
编辑:Adam Danz
2024-5-6
This appears to be a bug specific to the OpenGL renderer. Thanks for reporting this issue. Investigation will continue internally.
7 个评论
Walter Roberson
2025-2-27,21:36
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 ;-)
更多回答(1 个)
Sam Chak
2024-4-26
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 个评论
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!