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 Commenti

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.
Luis Vallejo Castro's "Answer" moved here:
Thank you, but now I want to reset the default colour order... but I cannot!
@Luis Vallejo Castro: what MATLAB version are you using?
@Stephen Cobeldick I use MatLab R2018b

Accedi per commentare.

Risposte (3)

Stephen23
Stephen23 il 15 Feb 2019
Modificato: Stephen23 il 18 Feb 2019

2 voti

"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 il 16 Feb 2019

2 voti

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 il 16 Feb 2019

0 voti

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)

Categorie

Prodotti

Commentato:

il 27 Ott 2020

Community Treasure Hunt

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

Start Hunting!

Translated by