How to flip the top x-axis so it matches the bottom plot?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Star Strider
2022년 10월 21일
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
Image Analyst
2022년 10월 21일
Not sure what you want but try using one of these after you've made the plot
axis ij
axis xy
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!