Xtick location is not correct

Hello,
I have two plots on one figure, which is shown in the attachment. The X tick for both of the the plots should be in the same location but they are not. Could someone please help me how I can fix this problem.
Thanks

2 Comments

MINA
MINA on 1 Jun 2016
Edited: dpb on 2 Jun 2016
ax1 = gca;
get(ax1,'Position');
set(ax1,'XColor','white',...
'YColor','white',...
'XTick',[],'XTickLabel',[]);
set(get(ax1, 'Ylabel'), 'String', 'Trials #','fontsize',10);
set(get(ax1, 'Xlabel'), 'String', 'Time (s)','fontsize',10);
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','white',...
'YColor','white',...
'XTick',[0:16],'XTickLabel',[0:16]);
set(get(ax2, 'Ylabel'), 'String', 'Average Speed (cm/s)','fontsize',10);
ax1 = gca; % be better to
ax(1)=axes; % create axes/save handle instead of gca
set(ax(1),'XColor','white',...
'YColor','white',...
'XTick',[],'XTickLabel',[]);
ylabel(ax(1),'Trials #','fontsize',10);
xlabel(ax(1),'Time (s)','fontsize',10);
ax(2)=axes('Position',get(ax(1),'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','white',...
'YColor','white',...
'XTick',[0:16]);
ylabel(ax(2),'Average Speed (cm/s)','fontsize',10);
What you're missing above is setting xlim to match the 'xtick' and then linking the two x-axes
linkaxes(ax,'x') % link the two x-axes to keep in synch
xlim(ax(2),[0 16]) % now set the limits to match where tried put ticks
Also notice used array to hold the handles instead of named variables--this way when need both can use the array name instead of building vector (in, say linkaxes)

Sign in to comment.

Answers (1)

You'll have to set the two axes xlim ranges to a consistent set of ticks and range. It's difficult to interpret but I guess both axes are actually time and the Speed is supposed to be a Title, not an axis label? If that is so, look at
doc linkaxes % to connect the two so they stay in synch irregardless of what do to one or t'other.
It's always easier to give more precise information when the code that generated the figure is shown; often experienced users here can spot "issues" causing such problems that aren't so apparent to the neophyte (else't they wouldn't have to be asking now, would they? :) )

1 Comment

That's right. Both of axes are time and the Speed is the title.

Sign in to comment.

Asked:

on 1 Jun 2016

Commented:

dpb
on 2 Jun 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!