Get next plot color

조회 수: 181 (최근 30일)
David C
David C 2011년 7월 11일
댓글: Alexandra Gallyas Sanhueza 2021년 4월 29일
When using the function plot with hold set to 'all', each call to plot uses the successive entry in the ColorOrder property of the current axes. Is there a way to find out what is the color the next call to plot will use, if it is not known how many calls to plot have already been executed?
In other words, here is an example to clarify my question: plot(x,bob) hold all plot(x,garry) ... (unknown number of calls to plot)
What will be the color of the next plot?
Thanks, David
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 7월 11일
There are many plotting functions that add multiple children -- though the top level child might be an hggroup .
plot() adds multiple line() objects; bar() adds multiple patch() objects; boxplot() adds a combination of objects; polar() adds a combination of objects; contour() adds an hggroup that has patch() objects and text() objects as its children...
David C
David C 2011년 7월 13일
Thank you for the input everyone.

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

채택된 답변

Daniel Shub
Daniel Shub 2011년 7월 11일
I think the number of children should equal the number of calls to plot. You need to use mod to loop through the colors (i.e if the number of plots is greater than the number of colors).
colorOrder = get(gca, 'ColorOrder');
plot(1:10, 'Color', colorOrder(mod(length(get(gca, 'Children')), size(colorOrder, 1))+1, :))
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 7월 11일
To deal with Walter's observation about plotyy, I guess you would need to check the figure for all children which are axes. You then might want to count the children of any axes which have identical positions to the axis you are interested in.
David C
David C 2011년 7월 13일
Your code works well with plot(), which is what I'm using. I like how your code handles the corner cases with mod perfectly.

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

추가 답변 (5개)

Jan
Jan 2011년 7월 11일
What about trying it:
lineH = plot(1,1);
color = get(lineH, 'Color');
delete(lineH);
[EDITED]: Walter's comment pointed me to the fact, that the intermediate creation of a PLOT line changes the next color. This is not working
  댓글 수: 3
David C
David C 2011년 7월 13일
Although not ideal for the purpose I had in mind, it is a good idea.
Alexandra Gallyas Sanhueza
Alexandra Gallyas Sanhueza 2021년 4월 29일
I was actually searching how to advance to the next color and this works :)

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


Teja Muppirala
Teja Muppirala 2011년 7월 13일
This seems to work ok:
figure
hold all;
plot(rand(5,3));
h = plot(nan,nan);
nextcolor = get(h,'color')
h = plot(nan(2,size(get(gca,'colororder'),1)-1)); %Loop back
delete(h)
plot(rand(1,10)) %<-- This line's color is "nextcolor"
  댓글 수: 3
David C
David C 2011년 7월 13일
This is very clever; thank you. As Daniel's answer was the first that worked for me, I have accepted his answer.
Jan
Jan 2011년 7월 13일
If the LineStyleOrder is not scalar, it should be considered also.

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


John Barber
John Barber 2011년 7월 14일
The next color to be used by a call to plot is stored as an index into the list of colors in the axes' ColorOrder property. You can access this index using:
NextColor = getappdata(hAx,'PlotColorIndex')
where hAx is the handle of the axes of interest. This is an undocumented feature, so it may not work in all MATLAB versions (I'm using R2010a / 7.10)

Jim Hokanson
Jim Hokanson 2017년 1월 9일
편집: Jim Hokanson 2017년 1월 9일
In newer versions of Matlab the state is stored in the axes as 'ColorOrderIndex'. In 2016b, this wraps, and you can get values from 1 to (n_colors+1) which after (n_colors+1) goes back to 2 (you only see 1 at the start of a plot, at least in this version).
So the next color is:
colors = get(gca,'ColorOrder');
index = get(gca,'ColorOrderIndex');
n_colors = size(colors,1);
if index > n_colors
index = 1;
end
next_color = colors(index,:);
  댓글 수: 1
J. Alex Lee
J. Alex Lee 2020년 4월 10일
Update in 2020 (not sure about previous versions), but it is now an exposed property in the axes:
ax = gca;
ax.ColorOrder
ax.ColorOrderIndex
Works for uiaxes() as well

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


Mauro
Mauro 2014년 8월 18일
to get the colour from the 1th to the 20th lineplot, type
cm = lines(20);
after the 7th line it starts again with blue [0 0 1]
so
figure(1)
clf
plot(randn(50,10)*0.1+repmat((1:10),50,1))
ist the same as
figure(1)
clf
cm = lines(10)
hold on
for k = 1:10
plot(randn(50,1)*0.1+k,'color',cm(k,:))
end

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by