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

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

See the documentation or related question .

What I would like to do is adjust the color order and style order, which will be applied independently.

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

+6
source share
2 answers

I see no way to do this right out of the box. The direct way is to manually set the color / style 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(ti/5)'; end h = plot(t,a); 

As you explained in your question, the default behavior is to scroll colors first and then line styles. If you want to apply them yourself, 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)) 

Perhaps you can wrap this in a function for easy access (you still have to pass descriptors to line graphic objects):

 function applyColorLineStyleIndependently(h) %# ... end 

enter image description here

+4
source

Amro fits well. As a note, you do not need to set default values โ€‹โ€‹for 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: mycolors () function is done at home. I define

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

(color names taken from 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 
0
source

Source: https://habr.com/ru/post/896746/


All Articles