MATLAB Answers

Change colour order and restore it

550 views (last 30 days)
Luis Vallejo Castro
Luis Vallejo Castro on 13 Feb 2019
Commented: Renn Kai on 27 Oct 2020
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 Comments
Luis Vallejo Castro
Luis Vallejo Castro on 18 Feb 2019
@Stephen Cobeldick I use MatLab R2018b

Sign in to comment.

Answers (3)

Stephen Cobeldick
Stephen Cobeldick on 15 Feb 2019
Edited: Stephen Cobeldick on 18 Feb 2019
"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 on 16 Feb 2019
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 on 16 Feb 2019
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)

Community Treasure Hunt

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

Start Hunting!

Translated by