Hi Owen,
From your question I understand that the number of ticks shown on the first axis is different than the number of ticks in the new axis that you have added even though the limits are as you would like them.
This is happening because based on the limits of the second axis, the plot is automatically selecting the tick labels. You can work around this issue by modifying the code a little bit. I have shown below how you can do that:
For the first axis and the second axis to have the same number of ticks, we need to specify the 'Xtick' parameter for one of the axis. We can do that by finding out how many ticks are there in the first axis. The steps are:
a) Find the Xticks for the first axis b) Calculate the length c) Create new array of Xticks for the second axis based on the range and number of ticks d) Set Xticks for the second axis
For simplicity, I am assuming the following data for x1, y1 and y2:
x1 = 1:0.01:2*pi;
y1 = cos(x1);
y2 = sin(x1);
The changes to the code are shown below (please look carefully at the portion marked with %new code):
[AX,H1,H2] = plotyy(x1,y1,x1,y2);
ax1 = gca;
delta = 0.1;
set(ax1, 'Position', get(ax1, 'Position') + [0 delta 0 -delta]);
ax2=axes('Position', get(ax1, 'Position').* [1 1 1 0.001]-[0 delta 0 0],'Color','none');
domain=get(ax1, 'XLim')
domain=fliplr(domain).^-1
val=get(ax1, 'XTick')
l = length(val)
ticks = linspace(domain(1),domain(2),l)
set(ax2,'Xtick',ticks);
set(ax2,'xdir','reverse','Xlim',domain);
I get the following output figure:
Best Answer