How to plot two graphs with different x axes on the same graph without using tiled layout?
3 次查看(过去 30 天)
显示 更早的评论
I am trying to overlay two sets of data, one with pixels as x axis and the other wavenumbers. I have tried the tiled layout and they work beautifully in MatLab, however, when exporting these graphs as images only one of the plots is exported/copied. Is there another way of achieving this? Thank you!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1691846/image.png)
Above: Figure as seen in MatLab
Below: Figure exported
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1691851/image.png)
1 个评论
Rik
2024-5-10
Have you tried creating two axes objects in the same place? It takes some careful tweaking to make sure no parts overlap, but it tends to export as expected.
采纳的回答
Cris LaPierre
2024-5-10
The plot created by the following example appears to export correctly
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x2,y2,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
% Export the plot
exportgraphics(gcf,'multipleAxes.png')
Here is the image that gets exported
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1691876/image.png)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!