"Column Vector" XTick labels

조회 수: 3 (최근 30일)
Mark Stone
Mark Stone 2022년 5월 28일
댓글: dpb 2022년 5월 30일
Consider the following plot:
plot(0:2,2-(0:2),'*'),xticks([0 1 2]),xticklabels({'(0,2)','(1,1}','(2,0)'})
I want to keep the "plot" as is, just change the labeling for the x axis ticks., so that each xtick label would be a column vector instead of a pair in parentheses. For instance,
0 1 2
2 1 0
instead of
(0,2) (1,1) (2,0)
Is this possible?

채택된 답변

Voss
Voss 2022년 5월 28일
편집: Voss 2022년 5월 28일
plot(0:2,2-(0:2),'*')
xticks([0 1 2])
xticklabels(sprintfc('%d\\newline%d',[0 2 1 1 2 0]))
  댓글 수: 5
Voss
Voss 2022년 5월 28일
@dpb: I also tried \n first. It seemed that MATLAB used \n to separate the labels:
xticks() % leaving xticks at its default, for the time being
ans = 1×6
0 0.2000 0.4000 0.6000 0.8000 1.0000
xtl = sprintfc('%d\n%d',[0 2 1 1 2 0])
xtl = 1×3 cell array
{'0↵2'} {'1↵1'} {'2↵0'}
xticklabels(xtl)
Then I remembered another answer I had seen recently, about emboldening a single xticklabel (an answer of yours, incidentally):
There, sending \bf to the TeX interpreter solved the problem, so I tried to do the same thing here but with a line break. I had to look up how to make a line break in TeX, and what I found was this question:
which suggested that \newline was the way to go, so I tried it out and it seemed to work.
dpb
dpb 2022년 5월 30일
I guess I had never looked that up before, or if had done in the past, had certainly forgotten it.
The undocumented sprintfc is handy, too; that replaces the functionality I got with the loop and explicit cast. I hadn't stumbled over it before, either...

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

추가 답변 (1개)

dpb
dpb 2022년 5월 28일
편집: dpb 2022년 5월 28일
I don't think so with the tick labels(*), but you can write them with text as
v1=[0:2];v2=[2:-1:0];
for i=1:3
xtl(i)=cellstr(sprintf('%d\n%d',v1(i),v2(i)));
end
xticklabels([])
text(xticks,repmat(-0.02,1,3),xtl,'VerticalAlignment',"top")
(*) When try to write string with embedded \n, the internals of xticklabels converts each to another string element so you end up with it thinking 2X the labels than number of ticks.
  댓글 수: 2
Mark Stone
Mark Stone 2022년 5월 28일
Thanks. Is the repmat'd value -0.02 a "magic" number which always works to make the alignment come out well? Does what that number "should be" depend on the numerical values in v1 and v2?
dpb
dpb 2022년 5월 28일
Yeah, it is "magic" number as shown -- because the default coordinates for text are in 'data' units. This can be set to be 'normalized' which are the units of the axes and then a fixed offset from the lower position number is the invariant way to place.

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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by