How can I add a second x-axis on top of the matlab figure for the same graph?
70 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-10-30
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');
采纳的回答
Voss
2023-10-30
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!