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.
Merge two plots with the same y axis
5 views (last 30 days)
Show older comments
Adi Purwandana
on 20 May 2024
Edited: Adi Purwandana
on 20 May 2024
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1698801/image.jpeg)
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 Comments
Accepted Answer
Piyush Kumar
on 20 May 2024
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';
More Answers (0)
See Also
Categories
Find more on Axis Labels 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!