How can I add a second x-axis on top of the matlab figure for the same graph?
43 views (last 30 days)
Show older comments
I have the following plot on matlab and want to add a second x-axis on top to show the corresponding distances with the same number of ticks and tick labels that can be found by:
distances (m) = (time_tau *10^-9) * 3*10^8
This is the plot:
% delay-spatial domain surf plot
figure
contourf( flip(time_tau), d_up, 20 .* log10(abs(zz_hts_tran_filtered_shift')), 'EdgeColor', 'none')
set(gca, 'FontSize', 16)
set(gcf, 'color', 'white')
%set(gca, 'YDir', 'reverse')
ylabel('$$\Delta_{\mathrm{\phi}} ~(^\circ)$$', 'Interpreter', 'Latex')
xlabel('$$\tau ~\mathrm{(ns)}$$', 'Interpreter', 'Latex')
zlabel('$$\hat{\sigma}_{\mathrm{eff}}~\mathrm{dB/m^2}$$', 'Interpreter', 'Latex')
xlim([first_limit, last_limit]);
colormap('jet'); % Choose your preferred colormap
colorbar('eastoutside');
4 Comments
Dyuman Joshi
on 30 Oct 2023
There's still an undefined variable.
Also, can you show what is the expected output/result?
load('time_tau.mat')
load('zz_hts_tran_filtered_shift.mat')
whos
% delay-spatial domain surf plot
figure
contourf( flip(time_tau), d_up, 20 .* log10(abs(zz_hts_tran_filtered_shift')), 'EdgeColor', 'none')
set(gca, 'FontSize', 16)
set(gcf, 'color', 'white')
%set(gca, 'YDir', 'reverse')
ylabel('$$\Delta_{\mathrm{\phi}} ~(^\circ)$$', 'Interpreter', 'Latex')
xlabel('$$\tau ~\mathrm{(ns)}$$', 'Interpreter', 'Latex')
zlabel('$$\hat{\sigma}_{\mathrm{eff}}~\mathrm{dB/m^2}$$', 'Interpreter', 'Latex')
xlim([first_limit, last_limit]);
colormap('jet'); % Choose your preferred colormap
colorbar('eastoutside');
Accepted Answer
Voss
on 30 Oct 2023
This creates text objects above the axes at the locations of the x-tick marks for the new x-tick labels, and another text object for the new x-label.
% random data:
time_tau = 1:100;
d_up = 1:10;
zz_hts_tran_filtered_shift = randn(100,10);
first_limit = 20;
last_limit = 65;
% delay-spatial domain surf plot
figure()
contourf( flip(time_tau), d_up, 20 .* log10(abs(zz_hts_tran_filtered_shift')), 'EdgeColor', 'none')
set(gca, 'FontSize', 16)
set(gcf, 'color', 'white')
%set(gca, 'YDir', 'reverse')
ylabel('$$\Delta_{\mathrm{\phi}} ~(^\circ)$$', 'Interpreter', 'Latex')
xlabel('$$\tau ~\mathrm{(ns)}$$', 'Interpreter', 'Latex')
zlabel('$$\hat{\sigma}_{\mathrm{eff}}~\mathrm{dB/m^2}$$', 'Interpreter', 'Latex')
xlim([first_limit, last_limit]);
colormap('jet'); % Choose your preferred colormap
colorbar('eastoutside');
% store some needed things:
ax = gca();
yl = ylim();
xt = xticks();
fs = get(ax,'FontSize');
% decrease the axes height a little, to make room for the new "xlabel"
ax.Position(4) = 0.93*ax.Position(4);
% create tick labels at the top of the plot:
text(xt,yl(2)*ones(numel(xt),1),string((xt *10^-9) * 3*10^8), ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','center', ...
'FontSize',fs)
% create a new "xlabel":
text(mean(xlim()),yl(2)+0.08*(yl(2)-yl(1)),'distances (m)', ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','center', ...
'FontSize',fs)
More Answers (0)
See Also
Categories
Find more on Labels and Annotations 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!