How to link x-axis of two plots in one single plot if they have diferent sizes
59 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Mohammad Sami
on 2 Jul 2021
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';
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!