MATLAB Answers

Plot with multiple x-axes - same scaling

59 views (last 30 days)
Mepe
Mepe on 20 Aug 2020
Commented: Mepe on 24 Aug 2020
Hello,
I am currently desperate to try to integrate two x-axes with identical y-axes in a plot ...
I have two problems here:
1.) How can I set the x ticks?
The lower axis should go (exactly) from 6.0 to 7.3 in 0.1 steps.
For the upper axis from 8.7 to 10 also in 0.1 steps. These are 14 points for both of them, but somehow that doesn't fit ...
2.) How can I adjust the x label of the lower axis?
Here my try:
yloss5MHz=[1.97203207600000;3.89411108500000;5.76765474000000;12.9537632700000;22.7141478100000;27.1717289300000;35.3378709500000;36.6057307600000;40.2620933300000;39.0743740300000;33.9838793500000;37.6049696900000;32.0796367400000;36.3937195200000]
yloss10MHz=[28.8049171100000;18.7450177100000;24.2295267100000;25.6980862100000;12.9537632700000;12.9537632700000;7.61531124600000;7.62594681400000;5.76765474000000;1.96074554000000;3.88304584800000;5.76765474000000;3.88304584800000;5.82188362800000]
xf5MHz=[6:0.1:7.3]
xf10MHz=[8.7:0.1:10]
f=figure (1)
ax1 = axes();
ax2 = axes('Position', get(ax1, 'Position'), ...
'XAxisLocation', 'top', ...
'xlim', [8.7 10], ...
'Color', 'none')
linkprop([ax1, ax2], {'ylim', 'Position'});
plot(xf5MHz,yloss5MHz,'Parent',ax1,'Color','k')
hold on
plot(xf10MHz,yloss10MHz,'Parent',ax2,'Color','k')
grid on
grid minor
legend('1f','2f')
xlabel('Frequency [MHz]')
ylabel('Amplitude loss [%]')
Thank you so much!

Accepted Answer

Cris LaPierre
Cris LaPierre on 22 Aug 2020
Edited: Cris LaPierre on 22 Aug 2020
If you haven't yet seen it, I'd recommend this documentation page. I also found this Answers post helpful.
Modifying your code, I came up with this.
yloss5MHz=[1.97203207600000;3.89411108500000;5.76765474000000;12.9537632700000;22.7141478100000;27.1717289300000;35.3378709500000;36.6057307600000;40.2620933300000;39.0743740300000;33.9838793500000;37.6049696900000;32.0796367400000;36.3937195200000];
yloss10MHz=[28.8049171100000;18.7450177100000;24.2295267100000;25.6980862100000;12.9537632700000;12.9537632700000;7.61531124600000;7.62594681400000;5.76765474000000;1.96074554000000;3.88304584800000;5.76765474000000;3.88304584800000;5.82188362800000];
xf5MHz=6:0.1:7.3;
xf10MHz=8.7:0.1:10;
figure
f1 = line(xf5MHz,yloss5MHz,'Color','k');
xlim([6 7.3])
xticks(xf5MHz)
xticklabels(string(xf5MHz))
ax1 = gca; % current axes
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YTick',[],...
'Color','none');
linkprop([ax1, ax2], {'ylim', 'Position'});
f2 = line(xf10MHz,yloss10MHz,'Parent',ax2,'Color','k');
xlim([8.7 10])
xticks(xf10MHz)
xticklabels(string(xf10MHz))
grid on
grid minor
legend([f1,f2],{'1f','2f'})
xlabel(ax1,'Frequency [MHz]')
ylabel(ax1,'Amplitude loss [%]')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by