Aligning a linear and non-linear x-axis

20 次查看(过去 30 天)
I have a spectroscopy plot of two curves, one relating to wavelength and one to wave number.
Wavenumber = 1/wavelegnth, so the relation is non linear. I want to align the top x-axis with bottom x-axis so that the peaks of the two curves are displayed in line with each other. Is this possible?
This is my current code:
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,wl1,sol1,'-r','LineWidth',3)
ax1.XAxisLocation = 'bottom';
ax1.YAxisLocation = 'left';
ax1.XColor = 'r';
ax1.YColor = 'r';
set(ax1, 'xlim',[300 400])
xlabel('wavelength (nm)',FontSize=18)
ylabel('Absorption',FontSize=18)
hold on
ax2 = axes(t);
plot(ax2,wn1,sol1_adj,'-k','LineWidth',3)
set(ax2, 'xlim',[250000 333333])
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('wavenumber(m^-^1)',FontSize=18)
ylabel('Absorption',FontSize=18)
set(gca, 'XDir','reverse')
ax1.FontSize = 16;
ax2.FontSize = 16;
  2 个评论
Matt J
Matt J 2024-2-19
Can't run your code. No input variables are provided.
William Rose
William Rose 2024-2-19
Is there a factor-of-ten error in one of the x-axis scales? Consider the wavenumber and wavelength for the right-most peak in the figure you provided. The black (wavenumber) peak is at about 2.65e5 m^-1. This corresponds to 3.77e-6 m = 3770 nm. But the red (wavelength) peak is at about 377 nm.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-2-19
编辑:Cris LaPierre 2024-2-19
The axes you are plotting into are linear, so I think the best solution would be to plot both plots using wavelength, and then convert the labels of to top x axis to wavenumber using xticklabels. Note that xticklabels displays the label as a string, not a numeric, so you need to format your number so that it appears as you want. I use sprintf below for that purpose.
If you are particular about which wavenumbers are shown, you can set the tick locations using xticks. Just remember to set the tick location using wavelength, not wavenumber.
The code might look like this. The first part just reproduces the issue.
BTW, one of your axes is off by a factor of 10 based on the equation you have shared.
x1 = (300:10:400)*1e-9;
x2 = 1./x1;
y=rand(1,length(x1));
% current aproach to show the issue
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y,'-r')
xlabel('Wavelength (m)')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x2,y,'k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('Wavenumber (m^{-1})')
ax1.XAxis.Exponent = -9;
ax2.XAxis.Exponent = 4;
Now here is the same plot but with the proposed solution
figure
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1,x1,y,'-r')
xlabel('Wavelength (m)')
ax1.XColor = 'r';
ax1.YColor = 'r';
ax2 = axes(t);
plot(ax2,x1,y,'--k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('Wavenumber (m^{-1})')
% desired wavenumber tick locations
wvnum = (3.3:-0.1:2.5)*1e6 % 1/m
wvnum = 1×9
1.0e+06 * 3.3000 3.2000 3.1000 3.0000 2.9000 2.8000 2.7000 2.6000 2.5000
% corresponding wavelength
wvlg_nmTk = 1./wvnum % nm
wvlg_nmTk = 1×9
1.0e-06 * 0.3030 0.3125 0.3226 0.3333 0.3448 0.3571 0.3704 0.3846 0.4000
ax1.XAxis.Exponent = -9;
xticks(ax2,wvlg_nmTk);
xticklabels(ax2,sprintf('%3.0f\n',wvnum/1e4)); % display in meters
  4 个评论
Anna
Anna 2025-12-18,12:16
Hi Cris, thank you so much. This code has been very helpful. I am just wondering how to adjust the x-axis limits. For example, I want to set the wavelength range from 400 to 1400 nm. Unfortunately, using xlim(ax2, [400 1400]) results in disappearing of the data from the top axis. Thank you! :)
Cris LaPierre
Cris LaPierre 2025-12-18,14:20
Please share a working example. Adding that to the code above does not remove anything from the plot.
However, at least in the OP, the intent was to align the top and bottom axes. Setting the limits of one axis without adjusting the other would possibly lead to misinterpretation of the data since wavenumber and wavelength are related.
wavelength = 300:1600; % in nm
wavenumber = 1e7./wavelength; % in cm-1
data = rand(1, length(wavenumber));
figure
t = tiledlayout(1,1);
ax1 = axes(t);
plot(ax1, wavelength, data, '-r')
xlabel('Wavenumber (10^3 cm^{-1})')
ylabel('Absorption (a.u.)')
ax2 = axes(t);
plot(ax2, wavelength, data, '--g')
ax2.XAxisLocation = 'top';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('Wavelength (nm)')
% desired wavelength tick locations
wvnum = [35 25 20 15 12 10 8 7]; % 1e3 cm^-1
wvnum = wvnum*1e3; % cm^-1
% corresponding wavelength in nm
wvlg_nmTk = 1e7./wvnum
wvlg_nmTk = 1×8
1.0e+03 * 0.2857 0.4000 0.5000 0.6667 0.8333 1.0000 1.2500 1.4286
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xticks(ax1,wvlg_nmTk);
xticklabels(ax1,sprintf('%3.0f\n',wvnum/1e3)); % display in 1e3 cm^-1
xlim(ax2, [400 1400])

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by