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 Center および File Exchange2-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