Plot with 2 corresponding x-axes
20 ビュー (過去 30 日間)
古いコメントを表示
Christopher Saltonstall
2018 年 5 月 22 日
編集済み: Christopher Saltonstall
2020 年 3 月 9 日
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 . 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 simplified data?
x1 = 1:10;
x2 = 1./x1;
y = -x1 + 10;
0 件のコメント
採用された回答
dpb
2018 年 5 月 23 日
編集済み: dpb
2020 年 3 月 9 日
Basics aren't difficult; I'll have to consider precisely how to do the upper axis ticks; I've got another appointment in town shortly so no time to piddle but I think you'll have to use linear scale and then compute the percentage for each major/minor tick because it's nonlinear and there's not builtin feature...but to get the general outline isn't too bad...
figure
hAx(1)=axes; % first axes
hAx(2)=axes('Position',hAx(1).Position, ... % 2nd on top of first
'color','none', ... % so will show through
'XAxisLocation','top', ... % move XAxis to top
'XDir','reverse'); % and flip LR
hAx(1).XLim=[0.2 1.8]; % first axes limits
eV=hb./hAx(1).XLim*6.242E18*1E6; % limits of 2nd in eV from first
hAx(2).XLim=sort(eV); % match the limits
set(hAx,{'YScale'},{'log'},{'YLim'},{[1E3 1E8]}); % log y axes
... I don't have time to work out the algebra just now, sorry...if you don't get it to work and somebody else doesn't come along to finish it up I'll try to look in again tomorrow.
ADDENDUM
OK, carrying on from above,
fneV=@(lam) hb./lam*6.242E18*1E6; % function for wavelength-->eV
fnWL=@(eV) hb./eV*6.242E18*1E6; % and vice versa...convenience
hAx(2).XLim=hAx(1).XLim; % use same limits to match up to
lamAx=fnWL([0.7:0.1:0.9 1:5]); % wavelengths for the wanted eV values
% draw ticks at matching wavelength, label with eV value
set(hAx(2),'XTick',sort(lamAx),'XTickLabel',fneV(lamAx))
hAx(2).FontSize=8; % need a little more room
hAx(1).YTick=[]; % remove superfluous axis ticks
box on % add the box around the figure
This yields the basic axes in which to draw as function of wavelength into hAx(1) that looks like
I believe the sample figure eV ticks don't quite line up at where they're supposed to be; looks to me like maybe they were approximated with a log axis instead??? Unless I made an error in one of the constants in defining the transformation equations but I don't believe I did...
This has only one really ugly artifact that is difficult to overcome in HG2; the "doubled-up" ticks on the lower x-axis; those are owing to box,'on' for hAx(2); doing that turns on the tick marks on the opposite axis from the primary one to match and since in this case the two scales don't align, the the ticks compete with each other. The only way I've imagined to solve this is to turn the box off for both axes and add yet a third to outline the plot, or draw a physical line to close the axes box. Anything done is an ugly kludge, however...
12 件のコメント
dpb
2020 年 3 月 9 日
編集済み: dpb
2020 年 3 月 9 日
I'll have to come back tonight but I'm thinking there may yet need to be another scaling that I left out of the above -- the above places the two endpoints correctly, but still linear between those based on the computed eV(lam) value. That returns the right numeric value, but that isn't the place it goes precisely on the nonlinear axes to actually correspond to the same location on the linear lambda axes.
It makes my head hurt to think about... :)
But, there's where the rub lies, scratch your head over the algebra and I'll look in again later.
Christopher Saltonstall
2020 年 3 月 9 日
編集済み: Christopher Saltonstall
2020 年 3 月 9 日
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!