MATLAB Answers

custom colors in plot (without loop)

12 views (last 30 days)
Rainer
Rainer on 31 Aug 2015
Commented: Rainer on 31 Aug 2015
Hi, suppose I have a custom color array:
c=[1 0 0
0 1 0
0 0 1]
Is there a way use the color array in a plot without looping? The following doesn't work:
plot(rand(100,3),'color',c)

  0 Comments

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 31 Aug 2015
Edited: Kelly Kearney on 31 Aug 2015
Another option is to set the color after plotting via the cell array set syntax:
c = [...
1 0 0
0 1 0
0 0 1];
h = plot(rand(100,3));
set(h, {'color'}, num2cell(c,2));
It requires an extra line of code, but I've always found this much easier than messing with default color order, hold settings, etc.

  1 Comment

Rainer
Rainer on 31 Aug 2015
Thanks, this is a great solution.

Sign in to comment.

More Answers (0)

Sign in to answer this question.