How can I add a second x-axis on top of the matlab figure for the same graph?

70 views (last 30 days)
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')
Unrecognized function or variable 'time_tau'.
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
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
Name Size Bytes Class Attributes ans 1x37 74 char cmdout 1x33 66 char time_tau 1x1594 12752 double zz_hts_tran_filtered_shift 1594x21 535584 double complex
% delay-spatial domain surf plot
figure
contourf( flip(time_tau), d_up, 20 .* log10(abs(zz_hts_tran_filtered_shift')), 'EdgeColor', 'none')
Unrecognized function or variable 'd_up'.
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');
demos serghiou
demos serghiou on 30 Oct 2023
Yes sorry, here is the rest of the variables and output figure.
first_limit = t_total(1) - 1.5;
last_limit = t_total(end) + 2.5;

Sign in to comment.

Accepted Answer

Voss
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)

Categories

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