How to flip the top x-axis so it matches the bottom plot?
5 views (last 30 days)
Show older comments
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1164793/image.png)
I also attached my data if you would like to use it.
0 Comments
Answers (2)
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);
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';
% 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')
.
0 Comments
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
0 Comments
See Also
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!