How to link x-axis of two plots in one single plot if they have diferent sizes
55 次查看(过去 30 天)
显示 更早的评论
Dear all,
I have the following plot. Y-axis corresponds to the depth and X-axis corresponds to the range
And I have also a second plot, where the Y-axis respresents the depth and X-axis represents the distance in km
The thing is that I need to combine both plots in one single figure (overlap the second one in the first one).
The problem is that the X-axis is different, and for this reason does not match correctly.
So, my question is, how can I overlap the second plot into the first one with a correct X-axis. The idea is to maintain the X-axis of the second plot.
Thanks in advance for your help.
0 个评论
回答(1 个)
Mohammad Sami
2021-7-2
This Matlab example describes how you can overlay a second Axes object on top of another axes object.
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); % create a tiled layout with only 1 tile
% create axes 1 and plot your first graph
ax1 = axes(t);
plot(ax1,x1,y1,'-r')
ax1.XColor = 'r';
ax1.YColor = 'r';
% create the second axes on the same tile
ax2 = axes(t);
plot(ax2,x2,y2,'-k')
% change the positions of the x any y axis so they do not overlap axes 1
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
% make the axes 2 transparent so you can see the axes 1
ax2.Color = 'none';
% Turn off the plot boxes to prevent the box edges from obscuring the x- and y-axes.
ax1.Box = 'off';
ax2.Box = 'off';
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!