How to make text in plot follow colormap?

조회 수: 13 (최근 30일)
Lucas Wong
Lucas Wong 2022년 5월 10일
편집: Lucas Wong 2023년 2월 25일
Hi everyone,
I am trying to make a plot where instead of a legend I have used text to annotate just above each line. Here is an example:
I have used a colormap (winter) to color each line in my plot by:
set(0,'DefaultAxesColorOrder', winter(5));
I have then placed text at the chosen position by selecting an x and y coordinate.
t = text(12 ,1.4, NCA-OCY,'FontSize',16); %...ect
Q: How would I go about applying the same colormap to my plot labels?
Thanks!

채택된 답변

DGM
DGM 2022년 5월 10일
편집: DGM 2022년 5월 10일
Consider the crude example:
% you have some plots already
plot(-0.5:4.5,0.5:5.5)
cmap = winter(5); % use the colormap
for k = 1:5
t(k) = text(0,k,sprintf('this is label #%d',k),'FontSize',16);
t(k).Color = cmap(k,:); % just set the color
end
Note that t is an array of graphics handles instead of a scalar handle. If you were to need to change the text objects later, you'd still be able to.
  댓글 수: 1
Lucas Wong
Lucas Wong 2022년 5월 11일
This worked great. Thanks for your help!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by