Plot with two related x-axes
Show older comments
This question may seem like it has been answered in the forums but I believe there is a subtlety that has not been directly answered. I want to plot the optical absorption coefficient of a material as a function of both photon wavelength (bottom x-axis) and energy (top x-axis) similar to this figure shown below. There are two problems with this task that I have not found sufficiently addressed. The first is to ensure that all of the points in the bottom axis line up with the points in the top axis (we need to some how link the two x-axes). Second, because of how energy and wavelength are related, one axis will be ascending while the other will be descending (Matlab doesn't like descending x-axes). How can you make the referenced plot? Can someone recreate the reference plot using the following data taken from the reference plot?

clear
close all
data = [0.35737704918032787, 92649850.48039015
0.3819672131147541, 72475211.53588514
0.41147540983606556, 55967196.301264346
0.4229508196721312, 51126728.207795605
0.4557377049180328, 47937263.23846833
0.5016393442622952, 43803474.996365294
0.539344262295082, 40544606.59504045
0.5688524590163934, 34277014.89316264
0.5950819672131147, 25461360.619704828
0.6229508196721312, 16618285.037019765
0.6655737704918033, 10037712.197086804
0.7213114754098361, 6724703.556947659
0.7950819672131147, 4169337.0144349397
0.8836065573770491, 2722604.5977291227
0.9557377049180328, 1997130.2156342946
1.0344262295081967, 1523018.3932672704
1.1049180327868853, 1255113.4937515096
1.1852459016393442, 1034416.372625131
1.2737704918032788, 886319.8712540427
1.359016393442623, 779309.6059234422
1.4540983606557378, 609961.3425615291
1.4918032786885247, 502533.5248888627
1.5098360655737708, 398203.214420678
1.5196721311475412, 299601.3683763488
1.5213114754098362, 195504.2893801002
1.5262295081967214, 119588.7737216253
1.5360655737704918, 65114.84416472715
1.5475409836065577, 38316.26954492593
1.5508196721311474, 25329.082065449114
1.559016393442623, 18331.533361062826
1.5688524590163935, 14713.897024069562
1.5934426229508198, 11361.99263934681
1.6196721311475408, 8773.786275376307
1.6508196721311474, 6433.761487810411
1.6754098360655738, 5032.800618487584
1.7049180327868854, 3787.196421050415
1.7327868852459019, 2886.942647505133
1.7557377049180327, 2287.679320744783
1.777049180327869, 1933.9100405245356];
%planks constant
h = 4.135e-15; %eV s
%speed of light
c = 3e8; %m/s
%Ge absorption coef
alpha = data(:,2); %1/m
%wavelength
lambda = data(:,1); %microns
%energy
E = h*c./(lambda*1e-6); %eV
figure(1)
plot(lambda,alpha,'r')
xlabel('Wavelength (\mum)')
ylabel('\alpha (m^{-1})')
set(gca,'Yscale','log')
figure(2)
plot(E,alpha,'r')
xlabel('Energy (eV)')
ylabel('\alpha (m^{-1})')
set(gca,'Yscale','log')
6 Comments
Adam
on 9 Mar 2020
What do you mean by 'Matlab doesn't like descending x-axes'?
The following works fine for me to reverse an x-axis, though obviously it is a trivial case.
figure; hAxes = gca;
hAxes.XDir = 'reverse'
I'm also not sure what you mean by the points on top and bottom x-axis 'lining up' (I don't really see in the link you gave an obvious example of what you are asking). Do you mean you want the ticks on the two axes to line up even though they are showing two different properties?
Christopher Saltonstall
on 9 Mar 2020
Edited: Christopher Saltonstall
on 9 Mar 2020
Adam
on 9 Mar 2020
Well, if you set the limits of both axes then they will line up. The exact ticks will likely not line up with ticks on the other axes, but if you want that too then you would need to edit the XTick and maybe XTickLabel properties as appropriate, as well as the XLim properties. Zooming in would be a problem though.
In which case your best bet would, perhaps be to have the exact same ticks for each axes and use only the XTickLabel property of the 2nd axis to give the true value. Then you can use
doc linkaxes
to ensure the ranges stay linked. I don't like using XTickLabels rather than actual XTick values like this myself because data cursor reporting and stuff like that is incorrect as it is based on the actual axis values, not any labels applied to the ticks.
Christopher Saltonstall
on 9 Mar 2020
Adam Danz
on 9 Mar 2020
Note that a similar solution was offered for the same question (and same OP) a while back,
Christopher Saltonstall
on 9 Mar 2020
Edited: Christopher Saltonstall
on 9 Mar 2020
Accepted Answer
More Answers (0)
Categories
Find more on Data Exploration 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!
