MATLAB: How to plot semilogy on two axes

plot two x axes semilogy

Dear All,
I have three sets of data (y1,2,3), and I want to plot them on a semilogarithmic (y) scale using the same x scale, but I want to use different ticks and tick labels for y1 and y2 (bottom) and y3 (top)x axes, generalizing it would be two x scales. I tried plotxx but linespec for line vs semilogy are not the same, so I cannot adapt plotxx for my case.
Based on prior posts, I tried this:
% import data into matlab
y1 = zeros(14,1);
y2 = zeros(10,1);
y3 = zeros(14,1);
x = 1:14;
% paste data in y1 (miss by distance), y2 (miss by indiv amplitude), y3 (reg),
% first, plot individual y data using editor to get an idea about ylim
% code break
set (0, 'DefaultAxesFontSize', 8,'DefaultAxesFontName', 'Times');
figure;
set (gcf, 'Units', 'centimeter');
pos = get (gcf, 'Position');
pos(3) = 10;
pos(4) = 8;
set (gcf, 'Position', pos);
set (gca, 'Units', 'centimeter');
set (gca, 'Position', [1.5 1.2 8 5]);)
h1 = semilogy(x,y1,'.r','MarkerSize',6);
hold on;
h2 = semilogy(x(1:10),y2,'xb','MarkerFaceColor','b','MarkerSize',8);
hold on;
h3 = semilogy(x,y3,'ok','MarkerSize',5);
hold off;
H1 = gca;
set(H1,'Xlim',[0.5 14.5]);
set(H1,'Ylim',[10^(-2) 10^3]);
set(H1,'XTick',1:14);
set(H1,'XTickLabel',{'0','5','10','15','20','25','30','35','40','45','50','55','60','65'});
box('on');
set(H1,'tickdir','out');
ax=axis;
H2=axes('position', get(H1,'position'));
axis(ax);
set(H2,'XAxisLocation','top')
set(H2,'XTick',1:14);
set(H2,'XTickLabel',{'reg1cm_1','reg1cm_2','reg1cm_3','reg1cm_4','reg1.5cm_1','reg1.5cm_2','reg1.5cm_3','reg1.5cm_4','reg1.5cm_5','reg1.5cm_6','reg1.5cm_7','reg1.5cm_8','reg1.5cm_9'});
set(H2,'YAxisLocation','right');
set(H2,'yticklabel','');
The top tick labels are not displayed using the above, please advise.
Many thanks,
Octavian

Best Answer

As surmised above, you never set the x-axis limits on the second axes...
...
H2=axes('position', get(H1,'position'));
set(H2,'XAxisLocation','top')
% Following line is missing...so the default limits are [0 1]
set(H2,'Xlim',[0.5 14.5]);
...