Assign different colors to multiple plots without loop

I have one array x and a multi-column array Y and would like to plot all Y#s on the same diagram against X and assign each plot a different color, coming from the color array named ColorSpec (e.g. ColorSpec=[0 0 0; 1 0 0, ...]). Although using a loop is always an option but since this will happen multiple times in the code I would like to avoid a loop for brevity. Is this possible?

 Respuesta aceptada

Here is one way:
ColorSpec = [0 0 0;
1 0 0;
0 1 0;
0 0 1];
% Pretend data
x = sort(rand(3,1));
y = rand(3,4);
figure
ax = axes;
ax.ColorOrder = ColorSpec;
ax.NextPlot = 'add';
plot(x,y)

4 comentarios

Saeid
Saeid el 8 de Dic. de 2018
Thanks!
Saeid
Saeid el 8 de Dic. de 2018
By the way, what if the number of the curves changes, meaning that we do not know in advance how many colors will be used in the final plot? I can extend the number of colors in ColorSpec to the maximum foreseeable number.
It is OK if ColorSpec has more rows than lines in the plot. The plot will just use the first rows as needed.
It is also OK if ColorSpec has fewer rows than lines in the plot. The plot will "recycle" colors from the beginning again.
Saeid
Saeid el 8 de Dic. de 2018
Oh, I didn't know that. That's pretty cool, thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 8 de Dic. de 2018

Comentada:

el 8 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by