Filter löschen
Filter löschen

setting ColorOrderIndex before 1st line is drawn?

48 Ansichten (letzte 30 Tage)
Ian am 3 Jun. 2015
Beantwortet: M. A. Hopcroft am 2 Okt. 2015
Per Matlab documentation "Why Are Plot Lines Different", we can reset the color order to restart by using (handle).ColorOrderIndex=1, as in:
y1=rand(1,10);y2=y1+1; y3=y2+1; %generate some data to plot
fprintf('index is %d\n',h.ColorOrderIndex);
fprintf('index is %d\n',h.ColorOrderIndex);
hold on;
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.
fprintf('index is %d\n',h.ColorOrderIndex);
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?

Antworten (2)

Mike Garrity
Mike Garrity am 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 am 2 Okt. 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');
And the new ColorOrder is used when I call "plot"
Hope that helps,


Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by