Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: set line's color and style order to be applied in parallel

When you set DefaultAxesColorOrder and DefaultAxesLineStyleOrder MATLAB will first cycle through all colors with the first style, then again through all colors with the second style and so on.

See this documentation or related question.

What I would like to do is to set color order and style order to be applied independently.

For example, if I set DefaultAxesColorOrder to [1 0 0; 0 1 0; 0 0 1] and DefaultAxesLineStyleOrder to '-|--|:', the lines will be 'r-','g-','b-','r--','g--','b--','r:','g:','b:'. I want lines to be 'r-','g--','b:'.

like image 503
yuk Avatar asked Sep 06 '11 20:09

yuk


People also ask

How do I change the order of colors in MATLAB?

Modify Color Order for Specified AxesCreate a tiled chart layout and plot three lines in the first tile. Call the nexttile function with a return argument to get the axes object for the second tile. Plot three lines in the second tile. Then get the color order matrix for the axes and return the output in C .

How do you change the plot color and style in MATLAB?

You can change the colors, line styles, and markers of plot objects by modifying the ColorOrder or LineStyleOrder properties of the axes, or by changing the SeriesIndex properties of the plot objects.

How can you change the line styles of MATLAB plots?

You can modify other aspects of lines by setting properties. For example, Line objects have a LineWidth property for controlling the line's thickness. To create a thicker line, you can specify the LineWidth as a name-value argument when you call the plot function. In this case, set the LineWidth to 3 .

How do you use RGB triplets in MATLAB?

Using RGB triplets to change colorsOne can specify colors using a vector that gives the RGB triple where in MATLAB, each of the three values are numbers from 0 to 1. Usually RGB colors have values from 0 to 255. You can use those numbers and divide the vector by 255 to use within MATLAB.


2 Answers

I don't see a way to do this directly out of the box. The straightforward way is to set the color/style manually for each line.

Here is a more automated solution. Let's start with an example taken from the documentation:

%# defaults are set sometime before
set(0, 'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1], ...
      'DefaultAxesLineStyleOrder','-|--|:')

%# do plotting as usual
t = 0:pi/20:2*pi;
a = zeros(length(t),9);
for i = 1:9
    a(:,i) = sin(t-i/5)';
end
h = plot(t,a);

As you explained in your question, the default behavior is to cycle through the colors first, then the line styles. If you want to apply them independently, try the following:

c = num2cell(get(0,'DefaultAxesColorOrder'),2);
l = cellstr(get(0,'DefaultAxesLineStyleOrder'));
set(h, {'Color'}, c(rem((1:numel(h))-1,numel(c))+1), ...
    {'LineStyle'}, l(rem((1:numel(h))-1,numel(l))+1))

You can maybe wrap that in a function for convenient access (you still have to pass the handles to the lines graphic objects):

function applyColorLineStyleIndependently(h)
    %# ...
end

enter image description here

like image 101
Amro Avatar answered Nov 13 '22 14:11

Amro


Amro's approach works well. Just as a note, you don't have to set the defaults to do this. You can do something like this

col = mycolors(); % defines RGB colors scaled to [0,1]

i = 1;
c(:,i) = col.royal_blue; i = i+1;
c(:,i) = col.crimson; i = i+1;
c(:,i) = col.medium_sea_green; i = i+1;
c(:,i) = col.coral; i = i+1;
c(:,i) = col.dark_magenta; i = i+1;

colord = num2cell(c',2);
lineord = {'-' '--' '-.'}'; 

set(h,{'Color'}, colord(rem((1:numel(h))-1,numel(colord))+1), ...
      {'LineStyle'}, lineord(rem((1:numel(h))-1,numel(lineord))+1))
set(h,'LineWidth',2)

Edit: the mycolors() function is home made. I define

colors.maroon = [128,0,0];
colors.dark_red = [139,0,0];
colors.brown = [165,42,42];
...

(the color names are from this http://www.rapidtables.com/web/color/RGB_Color.htm). Then I scale them to [0,1] via

c = fieldnames(colors);
for i = 1:numel(c)
    colors.(c{i}) = colors.(c{i})/255;
end
like image 40
gordon Avatar answered Nov 13 '22 13:11

gordon