Change colour order and restore it

Hi,
I want to change figure colour order. I use this
co = [0 0.4470 0.7410;
0.85 0.3250 0.098;
0.9290 0.6940 0.1250;
0.4940 0.1840 0.5560;
0.4660 0.6740 0.1880;
0.6350 0.0780 0.1840;
0.3010 0.7450 0.9330];
set(gca,'defaultAxesColorOrder',co)
get(gca,'colororder')
I only change the last row becuase I want this colour before. However, it does not work, I get this
ans =
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.3010 0.7450 0.9330
0.6350 0.0780 0.1840
If I use this
co = [0 1 1;
0 0.5 0;
1 0 0;
0 0.75 0.75;
0.75 0 0.75;
0.75 0.75 0;
0.25 0.25 0.25];
Everything works fine... So, what is wrong?
On the other hand, after change the order (if I could), I want to restore it and I use this:
ax = gca; ax.ColorOrderIndex = 1;
But I could no restore it... I plot a figure and I get the same result that previously...
Any help?
Best regards

4 comentarios

Adam
Adam el 13 de Feb. de 2019
defaultAxesColorOrder
should be set on the root if you want to change all plots.
ColorOrder
should be changed on an individual axes handle if you just want to change that one.
Stephen23
Stephen23 el 15 de Feb. de 2019
Luis Vallejo Castro's "Answer" moved here:
Thank you, but now I want to reset the default colour order... but I cannot!
Stephen23
Stephen23 el 15 de Feb. de 2019
@Luis Vallejo Castro: what MATLAB version are you using?
Luis Vallejo Castro
Luis Vallejo Castro el 18 de Feb. de 2019
@Stephen Cobeldick I use MatLab R2018b

Iniciar sesión para comentar.

Respuestas (3)

Stephen23
Stephen23 el 15 de Feb. de 2019
Editada: Stephen23 el 18 de Feb. de 2019

2 votos

"I want to change figure colour order"
Figures do not have a ColorOrder property, only axes do.
It is easy to change the axes' ColorOrder, but note that "high-level" graphics operations (e.g. plot, surf, line, etc) completely reset the axes back to their default settings in preparation for displaying the new data. And the ColorOrder is not applied to already existing objects (i.e. you have to set the ColorOrder and then plot). This means there are basically two ways to set the ColorOrder:
  1. set the graphics root's default value to the colormap you want, or
  2. set an axes' value to the colormap you want AND also change its NextPlot value to 'replacechildren' or 'add'.
and then plot your new data.
Here is an example of the second option:
>> X = 0:0.1:2*pi;
>> Y = bsxfun(@plus,sin(X),(1:7).');
>> plot(X,Y,'LineWidth',3)
>> map = get(gca,'ColorOrder')
map =
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.3010 0.7450 0.9330
0.6350 0.0780 0.1840
>> map([end-1,end],:) = map([end,end-1],:); % swap last two rows.
>> set(gca, 'ColorOrder',map, 'NextPlot','ReplaceChildren')
>> plot(X,Y,'LineWidth',3)
Looking at the first two lines it is clear that they have swapped color. And checking:
>> get(gca,'ColorOrder')
ans =
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
0.4660 0.6740 0.1880
0.6350 0.0780 0.1840
0.3010 0.7450 0.9330
Afterwards you can reset the ColorOrder using:
set(gca,'ColorOrder','factory')
Asieh Daneshi
Asieh Daneshi el 16 de Feb. de 2019

2 votos

write the following codes before plotting your resluts:
co = [0 0.4470 0.7410;
0.85 0.3250 0.098;
0.9290 0.6940 0.1250;
0.4940 0.1840 0.5560;
0.4660 0.6740 0.1880;
0.6350 0.0780 0.1840;
0.3010 0.7450 0.9330];
fig=figure;
set(fig,'defaultAxesColorOrder',co)
it worked for me. just adhere to the order of the codes.
Image Analyst
Image Analyst el 16 de Feb. de 2019

0 votos

You can save the default color order in advance if you want to change it and restore it. See my attached demo (which sets it but does not restore it though that is trivial)

Categorías

Más información sobre Graphics Object Programming en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 13 de Feb. de 2019

Comentada:

el 27 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by