In 2013 and earlier I could do this to compare multi-line plots:
% make up some data
X1 = rand(5,3);
X2 = X1+rand(5,3)*0.1;
plot(X1); % draw dataset 1
hold on;
plot(X2,':'); % compare with corresponding dataset 2
hold off;
This is broken in matlab 2014: the second set of lines colours don't match up with the first set.
I guess this is because the axes keep track of the colororder index when hold is on.
How can I reset the colororder index so that subsequent plots restart with color 1, as in previous matlabs? I'd really rather not have to go through a for loop to draw each of the lines!

 Respuesta aceptada

Sanjay Manohar
Sanjay Manohar el 26 de En. de 2015
Editada: Sanjay Manohar el 26 de En. de 2015

4 votos

Thanks all for your help. For anyone who wants to do this in future:
I finally got the answer by email from Claudette at Mathworks Documentation.
set(gca,'ColorOrderIndex',1)
will reset the colour order, so subsequent plot calls will use the same colour set.
So:
% make up some data
X1 = rand(5,3);
X2 = X1+rand(5,3)*0.1;
plot(X1); % draw dataset 1
hold on;
set(gca,'ColorOrderIndex',1)
plot(X2,':'); % compare with corresponding dataset 2
hold off;
will produce comparable solid and dotted lines.

Más respuestas (2)

Image Analyst
Image Analyst el 24 de En. de 2015

2 votos

Starting with R2014b you have to explicitly specify a color, otherwise it will use the "next" color in subsequent calls to plot. For example:
plot(X1, 'b-', 'LineWidth', 3); % draw dataset 1
hold on;
plot(X2,'r:', 'MarkerSize', 10); % compare with corresponding dataset 2
grid on;
You might also find it interesting to run my attached colororder demo.

8 comentarios

Sanjay Manohar
Sanjay Manohar el 24 de En. de 2015
Editada: Sanjay Manohar el 24 de En. de 2015
Thanks for reply but I don't think you understood my question. I am using the automatic colour ordering to draw a matrix of values -- like you have done in your demo. This means that with a single plot command, you obtain three lines with three colours. I want to draw a set of corresponding dotted lines on top of that. That is the functionality I am trying to achieve without a for loop if possible.
If you run my code in Matlab 2013 and 2014b you will see what I am talking about. I simply want to reset the "next" color in the colororder back to 1. This must surely be possible: presumably, the axis object is storing which index it is on as an internal property. It should be a matter of saying something simple like
set(gca,'NextColorOrderIndex',1)
or something.
Image Analyst
Image Analyst el 24 de En. de 2015
Editada: Image Analyst el 24 de En. de 2015
I don't think you can reset the color order index unless you call cla though I could be wrong, so you need to get the color order:
% Get the initial set of default plot colors.
defaultColorOrder = get(gca,'ColorOrder');
Then keep track of what color to use, say in a variable called colorIndex.
plot(X1, '-', 'color', defaultColorOrder(colorIndex,:));
hold on;
plot(X2, ':', 'color', defaultColorOrder(colorIndex+1,:)); % Or whatever...
Then do whatever you need to do and whenever you want to go back to the first color, just do this:
colorIndex = 1;
Sanjay Manohar
Sanjay Manohar el 25 de En. de 2015
Editada: Sanjay Manohar el 25 de En. de 2015
Thanks again. But getting the color order doesn't really help, since I'll need to write a for loop to go through all the lines. Are you implying that I now HAVE to use a for loop to do this simple task, which used to be a single command in all previous releases?
I also tried this syntax which doesn't seem to work. Is there a way to do something like this instead?
cols = jet(5);
plot( rand(6,5), 'color', cols )
Unfortunately won't allow colors as matrix. If there's no way to do this straightforward and common task without a loop, I'm very disappointed in the MATLAB team.
Image Analyst
Image Analyst el 25 de En. de 2015
No, you don't need a loop. But you have to specify the color if you want to specify the color. It doesn't just automatically give blue each call to plot() like it used to. It will cycle through a bunch of colors. If you want the first default color, you need to specify that.
Sanjay Manohar
Sanjay Manohar el 25 de En. de 2015
Editada: Sanjay Manohar el 25 de En. de 2015
I'm sorry but I still don't understand how to do this without looping through each line. If you try out my code
X1 = rand(5,8);
plot(X1); % draw dataset 1
You get 8 lines in different colours, using the default colororder.
This is what I want to achieve without looping through each line! I want to do it twice, once solid and once dotted, with the same colours each time. My original code works in old matlab. Do you know how to do this?
If I understood you right, I'd have to replace my original code with a for loop:
cm=colormap;
for i=1:size(X1,2)
plot( X1(:,i), cm(i,:) )
plot( X2(:,i), cm(i,:) , ':')
end
Is that what you were suggesting? If not could you show how it can be done without for loop?
thanks for your patience!
Image Analyst
Image Analyst el 25 de En. de 2015
Sorry, I didn't notice it was a 2D array. You can plot the lines and the markers both in one call to plot(), you don't need two or a loop:
plot( X2, cm, ':-')
Sanjay Manohar
Sanjay Manohar el 26 de En. de 2015
Still not understanding.
I have two sets of lines. Have you looked at the original code I posted? If you run my code in Matlab 2013, you will obtain the figure that Matz has posted below as a test. It shows two sets of lines, one for X1, one for X2, overlain, each in the same set of colours, but with different dash styles.
This is so that I can easily compare line set 1 with line set 2. I just want to be able to do this:
plot( X1 )
hold on
plot( X2 , ':' )
hold off
to where X1 and X2 are matrices, and that I can compare line 1 of X1, with line 1 of X2.
When I plot the second set of lines, it used to use the same colours as the first set. However, now since 2014b it does not! And I can't work out the easy way of achieving this seemingly common task.
Image Analyst
Image Analyst el 26 de En. de 2015
I don't have R2013b installed anymore. Post screenshots.

Iniciar sesión para comentar.

Matz Johansson Bergström
Matz Johansson Bergström el 24 de En. de 2015

1 voto

That's odd. I'm using Matlab R2014a and it seems to be working fine. Are you using Matlab R2014b?

2 comentarios

Image Analyst
Image Analyst el 24 de En. de 2015
It was changed. Now plot uses different colors each time.
Sanjay Manohar
Sanjay Manohar el 24 de En. de 2015
Ah yes it's R2014b ! Sorry.

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 24 de En. de 2015

Editada:

el 26 de En. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by