setting ColorOrderIndex before 1st line is drawn?

54 views (last 30 days)
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?

Answers (2)

Mike Garrity
Mike Garrity on 3 Jun 2015
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 on 2 Oct 2015
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,

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!