Insert xticks in bold in existing xticks
    조회 수: 13 (최근 30일)
  
       이전 댓글 표시
    
Hi,
I would like to make some change to the xticks of my plot without doing it manually.
So far i'm changing xticks of certain values by a text with :
names = {'text1'; 'text2'; 'text3'};
plot(...
set(gca,'xtick',[position1, position2, position3],'xticklabel',names)
The downside is that now I only have those 3 texts displaying in the x axis. I would like to have the default ticks PLUS those texts at those positions (in bold if possible). And just erase default ticks that are at those 3 positions.
Example if I have on the x axis the labels:
1, 2, 3, 4, 5, 6, 7, 8
I would like to have :
1, 2, text1, 4, 5, text2, 7, text3
How can I do this?
댓글 수: 0
답변 (1개)
  dpb
      
      
 2018년 9월 5일
        You've got to write the same number of tick labels as ticks and place the non-automatic text in the locations desired, explicitly. There's not sufficient syntax to write a portion of the 'XTickLabel' array alone.
hAx=axes;                          % make an axes to play with
xlim([1 8])                        % set limits
xlbl=xticklabels;                  % retrieve default labels
ix=[3 6 8];                        % define those to change
for i=1:length(ix)                 % make up the new ones for those locations...
  xlbl(ix(i))={num2str(i,'\\bfText%d')};
end
xticklabels(xlbl)                  % and write them back.
댓글 수: 3
  dpb
      
      
 2018년 9월 6일
				
      편집: dpb
      
      
 2018년 9월 6일
  
			doc xticklabels
 ...
xl = xticklabels returns the x-axis tick labels for the current axes.
 ...
Introduced in R2016b
If your release is earlier, then you'll have to use
xlbl=hAx.XTickLabel;
or if even earlier than R2014, then
xlbl=get(hAx,'XTickLabel');
Somehow you've got to get the existing tick label array to operate on.
hAx=axes;
just created an axis to demonstrate with; use the handle to the axes object you're trying to add the labels. For the current axes, use
hAx=gca;
to get the handle into a variable instead of calling the function every time; gca can (and will) change which axes is returned if focus is changed, saving the handle to a variable ensures you're writing to the one intended.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

