setting ColorOrderIndex before 1st line is drawn?

조회 수: 52 (최근 30일)
Ian
Ian 2015년 6월 3일
답변: M. A. Hopcroft 2015년 10월 2일
Per Matlab documentation "Why Are Plot Lines Different", we can reset the color order to restart by using (handle).ColorOrderIndex=1, as in:
x=1:10;
y1=rand(1,10);y2=y1+1; y3=y2+1; %generate some data to plot
figure(1);
h=subplot(2,1,1);
fprintf('index is %d\n',h.ColorOrderIndex);
plot(x,y1,x,y2);
fprintf('index is %d\n',h.ColorOrderIndex);
hold on;
h.ColorOrderIndex=1;
plot(x,y3);
fprintf('index is now %d\n',h.ColorOrderIndex);
And indeed this does work -- y1 and y3 will be blue, y2 will be red, and checking the value of ColorOrderIndex shows that it increases after each plotline; setting ColorOrderIndex to 4 in the above code, y2 will be purple. But if I try to set the ColorOrderIndex before the first line is drawn (e.g., to start with red) it seems to ignore it.
h=subplot(2,1,2);
fprintf('index is %d\n',h.ColorOrderIndex);
h.ColorOrderIndex=2;
plot(x,y2);
y2 comes out blue instead of red.
My goal, of course, is to coordinate colors across charts, so y2 in subplot 2 is colored the same as y2 in subplot 1.
I'm obviously missing something on how ColorOrderIndex works.
Q1: Why doesn't this work (what am I missing in my understanding)?
Q2: what is the proper way to accomplish my goal?

답변 (2개)

Mike Garrity
Mike Garrity 2015년 6월 3일
If hold is on, then plot resets the ColorOrderIndex. If you just add
hold on
before your call to plot, then it should do what you want.

M. A. Hopcroft
M. A. Hopcroft 2015년 10월 2일
I had the same problem, and I've found a solution. I found this in the R14 documentation:
If the hold state is off (when the NextPlot property is set to 'replace'), then high-level plotting functions such as plot reset the color order to the default colors before plotting. If you want to specify new colors for the color order and do not want high-level plotting functions to reset them, then set the NextPlot property to 'replacechildren'.
So I added the following to the setup portion of my code:
co = get(handles.axes2,'ColorOrder');
set(handles.axes2,'ColorOrder',circshift(co,-2,1));
set(handles.axes2,'NextPlot','replacechildren');
And the new ColorOrder is used when I call "plot"
plot(handles.axes2,mydata,...);
Hope that helps,

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by