815 views (last 30 days)

x = 1:3;

y = [1 2 3; 42 40 34];

plot(x,y,'Color', [0.5 0.5 0.5; 1 0 0])

produce an error:

Error using plot

Color value must be a 3 element numeric vector

Same with:

plot(x,y,'rg')

Error using plot

Error in color/linetype argument

This has bugged me for years, but I have circumvented it by unrolling the matrix into a number of vectors that I plot one at a time using whatever color I prefer. But is there no way to tell MATLAB (in a compact, readable form) what colors I would like it to use for whatever number of lines it will plot?

Kelly Kearney
on 10 Jan 2017

An alternative method would be to save the handles of the plotted data and set the colors via the array option of set. I find this method a lot less hassle than messing with ColorOrder and hold states:

x = 1:3;

y = [1 2 3; 42 40 34];

h = plot(x,y);

set(h, {'color'}, {[0.5 0.5 0.5]; [1 0 0]});

I often use the shortcut of using a colormap with num2cell to get the desired list of colors:

set(h, {'color'}, num2cell(jet(2),2));

Kelly Kearney
on 27 Mar 2017

The cell arrays are used because I'm using the cell array syntax for set, as described in the help text for that function (third option):

"set(H,pn,pv) sets the named properties specified in the cell array of strings pn to the corresponding values in the cell array pv for all objects specified in H. The cell array pn must be 1-by-N, but the cell array pv can be M-by-N where M is equal to length(H) so that each object will be updated with a different set of values for the list of property names contained in pn."

I could also set multiple properties using this syntax:

set(h, {'color', 'linewidth'}, [num2cell(jet(2),2) num2cell((1:length(h))')]);

Image Analyst
on 30 Oct 2011

In the help it says this:

"plot automatically chooses colors and line styles in the order specified by ColorOrder and LineStyleOrder properties of current axes. ColorOrder : m-by-3 matrix of RGB values

Colors to use for multiline plots. Defines the colors used by the plot and plot3 functions to color each line plotted. If you do not specify a line color with plot and plot3, these functions cycle through the ColorOrder property to obtain the color for each line plotted. To obtain the current ColorOrder, which might be set during startup, get the property value:

get(gca,'ColorOrder')

Note that if the axes NextPlot property is replace (the default), high-level functions like plot reset the ColorOrder property before determining the colors to use. If you want MATLAB to use a ColorOrder that is different from the default, set NextPlot to replacechildren. You can also specify your own default ColorOrder."

co = get(gca,'ColorOrder') % Initial

% Change to new colors.

set(gca, 'ColorOrder', [0.5 0.5 0.5; 1 0 0], 'NextPlot', 'replacechildren');

co = get(gca,'ColorOrder') % Verify it changed

% Now plot with changed colors.

x = 1:3;

y = [1 2 3; 42 40 34];

plot(x,y, 'LineWidth', 3);

The things I put in the set() command are especially important.

Cici Ma
on 9 Jan 2017

A note for subplot: need to put the set line before each plot command

x = 1:3;

y1 = [1 2 3; 42 40 34];

y2 = [3 5 8; 11 17 29];

subplot(2,1,1);

set(gca, 'ColorOrder', [0.5 0.5 0.5; 0.2 0.2 0.2],'NextPlot', 'replacechildren');

plot(x,y1, 'LineWidth', 3);

subplot(2,1,2);

set(gca, 'ColorOrder', [0.5 0.5 0.5; 0.2 0.2 0.2],'NextPlot', 'replacechildren');

plot(x,y2, 'LineWidth', 3);

And much appreciated to Image Analyst!

Daniele Maddaluno
on 8 Dec 2017

Edited: Daniele Maddaluno
on 8 Dec 2017

I can solve your problem with something like this:

x = 1:5;

y = [x; x.^2; x.^3; x.^4; x.^5];

n = size(y, 1);

colors = hsv(n);

h = plot(x, y);

set(h, {'color'}, num2cell(colors, 2));

Vivek Bhardwaj
on 14 Feb 2019

Joakim Wang Erlandsson
on 19 Sep 2018

Jim Camp
on 4 Nov 2018

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/19815-explicitly-specifying-line-colors-when-plotting-a-matrix#comment_385548

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/19815-explicitly-specifying-line-colors-when-plotting-a-matrix#comment_385548

Sign in to comment.