Generating arbitrary colored tick labels

조회 수: 9 (최근 30일)
Leo Simon
Leo Simon 2020년 4월 17일
댓글: dpb 2020년 4월 17일
From this thread I learned from @FrankZalkow how to assign different colors to different tick labels. (I needed to use xticklabel_rotate to do this, because by default matlab just destroys the ticks, but I can live with this.) I want to extend his approach to produce arbitrarily colored labels. From the manual xcolors.sty, I learned how to insert into the \color{} command an arbitrary set of colors, i.e., \color{rgb:red,X;green,Y;yellow,Z} This does the trick in latex, but it doesn't work in matlab. Here's an mwe. If you set n=2 and run the code, you see a couple of colored \lambda's, but when n=3 it doesn't work. Can somebody suggest how to make it work please?
As a side question, I'm curious why even with n=2, it doesn't work without using xticklabel_rotate. Thanks for any advice.
n=3;
plot(1:n);
XTickLabel{1} = '\color{red} \lambda' ;
XTickLabel{2} = '\color{blue} \lambda' ;
if n==3;
XTickLabel{3} = '\color{rbg:red,5;green,6;yellow,1} \lambda' ;
end
set(gca,'XTick',[1:n]);
set(gca,'XTickLabel',XTickLabel);
xticklabel_rotate([],90)

채택된 답변

Tommy
Tommy 2020년 4월 17일
Is TeX an option for you?
n=10;
plot(1:n);
labels = cell(n,1);
for i=1:n
labels{i} = sprintf('\\color[rgb]{%f, %f, %f}%s', [rand rand rand], '\lambda');
end
set(gca,'XTick',1:n);
set(gca,'XTickLabel',labels);
Note that this requires rgb values, with each value between 0 and 1.
Also: what is xticklabel_rotate? Whatever it is, the above works for me without it.
  댓글 수: 2
Leo Simon
Leo Simon 2020년 4월 17일
Thanks @Tommy. Works perfectly. The reason why I couldn't get the program to work, ironically, was because my default setting in startup.m was to set the defaultAxisTickLabelInterpreter to be 'latex'! Seems very odd that this had the opposite effect. xticklabel_rotate is a wonderful utility here which allows you to rotate tick labels. couldn't live without it.
dpb
dpb 2020년 4월 17일
The property X|Y|ZTickLabelRotation has existed at least since HG2 (R2014a/b?).
Functions x|y|ztickangle were added in R2016b

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by