How can I change the color of each text in a plot?

4 次查看(过去 30 天)
Hello,
I'm trying to set the color of each text instance in a plot to a different value as shown:
C={[112 48 160], [85 142 213], [250 0 0], [146 208 80]}; %my color values
hcolors=cellfun(@(x) x/255,C,'UniformOutput',false)'; %colors converted to correct scale
set(h,{'Color'},hcolors) %sets the color of each line in h, this works fine
gtext=text(0.55*exes,maxes,{'Color'},hcolors);
However, when I try to set the color of the text according to the values in hcolors, I get the following error:
"Value cell array handle dimension must match handle vector length."
If I instead use
set(gtext,{'Color'},hcolors)
Everything works fine.
Is there a way to get the correct color output all in one line using text()?

采纳的回答

Adam Danz
Adam Danz 2019-8-23
编辑:Adam Danz 2019-8-23
This line below should work iff the length of 'h' equal 4.
set(h,{'Color'},hcolors)
% iff
length(h) == 4
This line below, however, will definitely fail because it does not include any text values. Even when a cell array of string values is added, you cannot assign multiple colors. Multiple colors can be assigned, however, using the method above.
gtext=text(0.55*exes,maxes,{'Color'},hcolors); % will result in error
gtext=text(x,y,text); % this should work
set(gtext,{'Color'},hcolors)
  2 个评论
Sam Malamis
Sam Malamis 2019-8-23
编辑:Sam Malamis 2019-8-23
Thanks for the help, I accidentally left out the cell array of string values, the line should read
gtext=text(0.55*exes,maxes,txt,{'Color'},hcolors), where txt is the cell array of 4 string values.
Like you said, I still get the error, so I will use set() for the colors.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by