How to flip the top x-axis so it matches the bottom plot?

5 views (last 30 days)
Hello,
I'm trying to plot a power spectra map with two x-axes. The bottom x-axis should be frequency and the top x-axis should be wavelength. Is there any way to match the x-axes so they match since the two variables are not directly proportional (Frequency = 1/Wavelength)?
Here is my code:
figure(666)
% setup bottom axis
ax = axes();
hold(ax);
ax.XAxis.Scale = 'log';
xlabel(ax, 'Frequency', 'Interpreter', 'latex', 'FontSize', 14);
ylabel(ax, 'Power', 'Interpreter', 'latex', 'FontSize', 14);
ax_top = axes();
hold(ax_top);
ax_top.XAxisLocation = 'top';
ax_top.YAxisLocation = "right";
ax_top.YTick = [];
ax_top.Color = 'none';
xlabel(ax_top, 'Wavelength', 'Interpreter', 'latex', 'FontSize', 14);
linkprop([ax, ax_top],{'Units','Position','ActivePositionProperty'});
ax.XLim = [0.01 0.5];
ax_top.XLim=[0 50];;
ax_top.XAxis.MinorTick = 'off';
semilogx(ax, F, Spec, '-k', 'LineWidth', 3); hold on
plot(ax_top,L,Spec,'-k','LineWidth',2); hold on
xline(ax,fs(1),'-r','LineWidth',2); hold off
legend('Power Spectrum','','Speration Frequency')
And here is my resulting plot
I also attached my data if you would like to use it.

Answers (2)

Star Strider
Star Strider on 21 Oct 2022
I am not certain what you want.
One option —
ax_top.XAxis.TickLabels = 1./(ax_top.XAxis.TickValues); % <— ADDED
That takes the current top x-tick values, keeps them as they are, and simply labels them as the inverses of the current values.
LD = load(websave('Data','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1164788/Data.mat'));
F = LD.F;
L = LD.L;
Spec = LD.Spec;
fs = LD.fs;
figure(666)
% setup bottom axis
ax = axes();
hold(ax);
Current plot held
ax.XAxis.Scale = 'log';
xlabel(ax, 'Frequency', 'Interpreter', 'latex', 'FontSize', 14);
ylabel(ax, 'Power', 'Interpreter', 'latex', 'FontSize', 14);
ax_top = axes();
hold(ax_top);
Current plot held
ax_top.XAxisLocation = 'top';
ax_top.YAxisLocation = "right";
ax_top.YTick = [];
ax_top.Color = 'none';
xlabel(ax_top, 'Wavelength', 'Interpreter', 'latex', 'FontSize', 14);
linkprop([ax, ax_top],{'Units','Position','ActivePositionProperty'});
ax.XLim = [0.01 0.5];
ax_top.XLim=[0 50];;
ax_top.XAxis.MinorTick = 'off';
% getax_top = get(ax_top)
ax_top.XAxis.TickLabels = 1./(ax_top.XAxis.TickValues); % <— ADDED
semilogx(ax, F, Spec, '-k', 'LineWidth', 3); hold on
plot(ax_top,L,Spec,'-k','LineWidth',2); hold on
xline(ax,fs(1),'-r','LineWidth',2); hold off
legend('Power Spectrum','','Speration Frequency')
Warning: Ignoring extra legend entries.
.

Image Analyst
Image Analyst on 21 Oct 2022
Not sure what you want but try using one of these after you've made the plot
axis ij
axis xy

Categories

Find more on Graphics Object Properties 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!