Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

plot - New line in axis tick labels in Matlab

Is there a way to have a new line in an axis tick label in Matlab to produce a multiline tick label?

The two suggestions from here for other text elements don't seem to work:

set(gca,'xticklabel',{{'line1','line2'}}) 

fails, and

set(gca,'xticklabel',{['line1' 10 'line2']}) 

or

set(gca,'xticklabel',{['line1' 13 'line2']}) 

ignore the newline or carriage return. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure how long it has been around, but at least in R2015b the axes objects have a 'TickLabelInterpreter' property, which can be specified to set how the tick labels are interpreted. If you choose a LaTeX interpreter, it is possible to have multiline ticklabels quite easily by wrapping them in a tabular environment.

Example:

figure;
plot(rand(10,1));
%// Tick label with arbitrary number of lines in tabular environment
xtl = 'egin{tabular}{c} line 1 \ line 2 \ line 3\ line 4end{tabular}';
%// use the tick label at location 5 on the x axis
set(gca,'xtick', 5, 'XTickLabel', xtl, 'TickLabelInterpreter', 'latex');

Output:

Multiline tick label

Of course, the drawback here is that you need to use the LaTeX interpreter which somewhat changes the appearance of the figure. But I believe some people (such as me) actually prefer the way the LaTeX interpreted figure annotations look! As an added bonus, you can use whatever other LaTeX markup you desire (equations, etc.) in the labels.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...