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

58 次查看(过去 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.

回答(1 个)

Mohammad Sami
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';
  1 个评论
Ricardo Duarte
Ricardo Duarte 2021-7-2
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by