How to link x-axis of two plots in one single plot if they have diferent sizes

41 views (last 30 days)
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.

Answers (1)

Mohammad Sami
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';
  1 Comment
Ricardo Duarte
Ricardo Duarte on 2 Jul 2021
It's not exactly that what I want. I don't want two different axis.
What I need is to "synchronize" both plots in the same x-axis, but the problem is that the plot on top has it x-axis between [0 and 100] and the second between [-1060 and -940].
This means that I want make 0 equal to -1060 and 100 equal to -940.
I wrote this code:
%create a new vector with the same length than the x-axis of the first plot
minlon=min(lon_km);
maxlon=max(lon_km);
newaxe=linspace(minlon:maxlon:200); %200 is the length of the x-axis of the first plot
%overlapping the plots.
ax1=axes;
surf(ax1,newaxe,-zt,tlt); %zt=depth; tlt=transmission loss
ax2=axes;
plot3(ax2,Xi,Yi,Zi);
ax2.Visible='off';
I obtain this:
However, I'm not sure that it is correct. Since the x-axis is bigger in this figure than in the first one.
Thanks.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!