Merge two plots with the same y axis

조회 수: 19 (최근 30일)
Adi Purwandana
Adi Purwandana 2024년 5월 20일
편집: Adi Purwandana 2024년 5월 20일
Hello there,
I have two sets of parameter, x1 and x2 and they both vary with depth. Since the scale span of x1 is different to that of x2, what I want to do is to put the axis of x2 above, as the secondary plot (see fig below). Anyone can help me to fix this?
x1 = 0:0.1:5;
y1 = 0:1:50;
y2 = 0:1:90;
x2 = y2.^0.9;
figure;
subplot(1,2,1)
plot(x1,y1);
ylabel('Depth (m)');
xlabel('Length');
subplot(1,2,2);
plot(x2,y2,'r');
ylabel('Depth (m)');
xlabel('Power');
Please find attached the data, or please access the code above to generate the data.
Thanks
  댓글 수: 2
Adi Purwandana
Adi Purwandana 2024년 5월 20일
Have you tried it? It's for yy axis not the x axis. I read already that link before and I can't manage it. Let me know if you try and have the solution.
Rik
Rik 2024년 5월 20일
A recent question also asked about two x axes in one plot. Where did you search?

댓글을 달려면 로그인하십시오.

채택된 답변

Piyush Kumar
Piyush Kumar 2024년 5월 20일
Hi Adi,
It seems like you want x2 to be plotted against y2 but have its x-axis aligned at the top nd y-axis synchronized with the y1.
x1 = 0:0.1:5;
y1 = 0:1:50;
y2 = 0:1:90;
x2 = y2.^0.9;
t = tiledlayout(1,1);
% Create the first axes
ax1 = axes(t);
plot(ax1, x1, y1, '-r');
ax1.XColor = 'r';
ax1.YColor = 'r';
% Create the second axes
ax2 = axes(t);
plot(ax2, x2, y2, '-k', 'Parent', ax2);
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
% Link the y-axes of ax1 and ax2
linkaxes([ax1, ax2], 'y');
ax2.YTick = [];
% ax2.YColor = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
I have tried it almost the same way as it is mentioned in the "Display Data with Multiple x-Axes and y-Axes" section of the link. Also, I have used linkaxes function to synchronize the two y-axis.
  댓글 수: 1
Adi Purwandana
Adi Purwandana 2024년 5월 20일
편집: Adi Purwandana 2024년 5월 20일
Super! Thank you Piyush, I really appreciate your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by