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

yuk picture yuk · Sep 6, 2011 · Viewed 10k times · Source

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:'.

Answer

Amro picture Amro · Sep 7, 2011

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