Change colour order and restore it

조회 수: 358 (최근 30일)
Luis Vallejo Castro
Luis Vallejo Castro 2019년 2월 13일
댓글: Renn Kai 2020년 10월 27일
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
Stephen23
Stephen23 2019년 2월 15일
@Luis Vallejo Castro: what MATLAB version are you using?
Luis Vallejo Castro
Luis Vallejo Castro 2019년 2월 18일
@Stephen Cobeldick I use MatLab R2018b

댓글을 달려면 로그인하십시오.

답변 (3개)

Stephen23
Stephen23 2019년 2월 15일
편집: Stephen23 2019년 2월 18일
"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 2019년 2월 16일
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 2019년 2월 16일
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)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by