How can I assign different colors to each plotted line?

5 views (last 30 days)
Hi,
I have the following code:
>>x=1-by-3 matrix
>>y=3-by-9 matrix
>>figure
>>cc=hsv(9);
>>plot(x,y,'color',cc);
The objective is to plot 9 lines using 3 points for each line. The code does that. However, I'm unable to assign different color to each line. I get the following error message: "Color value must be a 3 element numeric vector"
What am I doing wrong?
Amit

Answers (1)

dpb
dpb on 3 Dec 2014
Edited: dpb on 4 Dec 2014
At the top level of plot can't pass the array; only a single color vector.
Save the line handles and set their properties...
hL=plot(x,y,'k'); % just so see what happens later...
for i=1:length(hL),set(hL(i),'color',cc(i,:));end
NB: must reference the vector of the row of the colormap array.
Seems like should be able to use the vector syntax but set isn't apparently smart enough--
set(hL,{'color'},{cc})
fails as all other perturbations of syntax I can think of do as well. Documentation demonstrates some string cases like 'Tag' property by no numeric ones so is of no help.
ERRATUM
The above presumption against set is wrong; it's bad syntax on my part not clearly interpreting the documentation despite reading it...as the followup sidebar with IA shows, I finally figured out the problem. For posterity, the key is the cell array itself must have the proper dimensions, not the array being held in a cell.
set(hL,{'color'},{cc}) % fails, wrong because size(cc)=[1,1]
Instead use
set(hL,{'color'},mat2cell(cc,length(cc),1)) % success!!
or return the color array (or whatever property in general) as the cell by doing the conversion when creating it--
cc=mat2cell(colormap(9),9,1);
set(hL,{'color'},cc)
where used constants for the sizes to minimize parentheses-nesting.
  5 Comments
Image Analyst
Image Analyst on 3 Dec 2014
You can use set to set multiple different parameters, like 'Color' and 'LineWidth', by passing them in separately, or in cells . But you cannot sort of automatically increment the handles in the same set statement. Here's an illustration:
numberOfElements = 10;
x = 1 : numberOfElements;
numberOfCurves = 8;
y = rand(numberOfElements, numberOfCurves);
hLines = plot(x,y,'k'); % just so see what happens later...
% cm = rand(numberOfCurves, 3); % Random colors.
cm = jet(numberOfCurves); % Predefined "jet" colormap.
for k = 1 : length(hL)
% set(hLines(i), 'Color', cc(i,:), 'LineWidth', 3); % Works
set(hLines(k), {'Color', 'LineWidth'}, {cm(k, :), 3}); % Also works
end
% This will not work to set all hLines objects at once
% to different colors:
set(hLines, {'Color', 'LineWidth'}, {cm, 3}); % Fails
dpb
dpb on 3 Dec 2014
Edited: dpb on 4 Dec 2014
From
doc set
....
set(H,pn,MxN_pv) sets n property values on each of m graphics objects, where m = length(H) and n is equal to the number of property names contained in the cell array pn. This allows you to set a given group of properties to different values on each object.
...
Oh, wait a minute...there's the rub in that the cell content has to be the value and I put into a single cell instead of a cell array...
Going back to OP's example...
>> cc=colormap(hsv(9));
>> for i=1:length(cc),ccc(i,1)={cc(i,:)};end
>> whos ccc
Name Size Bytes Class Attributes
ccc 9x1 756 cell
>> hL=plot(rand(3,9),'k-');
>> set(hL,{'color'},ccc)
>>
Joy ensues!!! Works as should...I had kept using
ccc=mat2cell(cc);
or variations thereon that all still led to size(ccc)=1x1, NOT 9x1 even though content of the cell was length()=9
I'm glad to have finally fingered this out -- I went thru a lot of similar gyrations on the last month's worth of financial records evaluation I did generating a lot of historical views and fixed up a whole series of figures with varying numbers of lines to have consistent coloring across the group. The above would have shortened the code significantly but never figured out where I was making the error until now.
ADDENDUM
So the end result for clarity and succinctness is one can use
cm = mat2cell(jet(nCurves),nCurves,1);
hLines = plot(x,y,'k');
set(hLines, {'Color', cm);
I've not tested but I presume one can also use something like your last example above and do multiple properties at one time as well.

Sign in to comment.

Categories

Find more on Graphics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!