MATLAB: How to add a second , upper x axis and control the value’s locations and text

x axis

Hi all,
My code generates simple plot and I would like to add a second x-axis with the same scaling as in the first one. However, I cannot seem to control the locations and text where values are placed, from some reason. I would be happy to get your help.
Here is my code:
x = 1:10
y = x.^2;
%
plot(x,y)
xlabel('vector 1')
ylabel('vector 2')
ax1 = gca;
% handle second X-axis
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');
k=get(ax1,'XTick');
l=get(ax1,'Position');
set(ax2,'YTick',([]));
Thanks!
Ron

Best Answer

Are you trying to do something like this
x = 1:10;
y = x.^2;
%
ax1 = axes();
box(ax1);
xlabel('vector 1')
ylabel('vector 2')
plot(x,y)
% handle second X-axis
ax2 = axes('Position', get(ax1,'Position'), ...
'XAxisLocation','top', ...
'Color','none', ...
'XColor','k');
ax2.YAxis.Visible = 'off';
ax2.XLim = ax1.XLim;
also add the following line if you want the x-limits to be automatically linked
linkprop([ax1 ax2], 'XLim')
Related Question