Xtick location is not correct

조회 수: 5 (최근 30일)
MINA
MINA 2016년 6월 1일
댓글: dpb 2016년 6월 2일
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
MINA
MINA 2016년 6월 1일
편집: dpb 2016년 6월 2일
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);
dpb
dpb 2016년 6월 2일
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)

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2016년 6월 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
MINA
MINA 2016년 6월 1일
That's right. Both of axes are time and the Speed is the title.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by