Automating line markers, colors...

조회 수: 8 (최근 30일)
dpb
dpb 2014년 10월 10일
댓글: dpb 2014년 10월 10일
When use plot with an array by column, plot automagically cycles colors but when adding a series of lines with hold on after the first the color does not cycle.
One can, of course, create an array and set the colors manually but that becomes old quickly. The question is, then--
Is there a way to simply request the next color and/or marker in the internal list without having to build the arrays to pass? Or, equivalently, is there some combination of properties that can be set that will keep the parts of hold one wants (namely the axes limits, previous lines retained, etc., etc., ...) but yet let the color cycle? Of course, ideally the first of simply being able to reference/increment an index in the internal cycle table modulo size of table would be ideal...

채택된 답변

Star Strider
Star Strider 2014년 10월 10일
편집: Star Strider 2014년 10월 10일
This is what I always do:
x = linspace(1,2*pi,250);
v = 1:5;
y = cos(v'*x);
h = colormap(jet(5));
figure(1)
plot(x,y(1,:),'Color', h(1,:))
hold on
for k1 = 2:5
plot(x,y(k1,:), 'Color',h(k1,:))
end
hold off
grid
Slightly cumbersome, but automated and effective. Experiment with the colormap to get the result you want.
You probably have to build the marker array yourself.
NOTE — R2014b now cycles the colours without having to do anything special.
  댓글 수: 5
Image Analyst
Image Analyst 2014년 10월 10일
If you didn't want to "create an array and set the colors manually", and wanted to do it "without having to build the arrays to pass" then I'm not sure how "plot(x,y(k1,:), 'Color',h(k1,:))" meets either of those requirements. It does build up an array and pass in each color manually on each call. With the default color order method you still need to build up the colors that you want, but at least you don't need to pass them in, so at least it meets one of your requirements. I thought dpb would have known about Star's method of passing in colors from an array already. But whatever....it's easy enough to do whatever method you use.
dpb
dpb 2014년 10월 10일
I knew about the array-passing yes, hadn't thought of the automation of retrieving the array via colormap was the enhancement, IA. I'd always been building the character array or other other manually; it was that process I was trying to avoid.
Passing the array isn't too big a deal it was the case of when one is doing something somewhat stupid and hasn't thought of the logical answer one tends to presume others are doing the same or similar so the question is couched from that viewpoint. The above about "create an array ... manually" was from the standpoint I'd been writing something like
clrs={'r';'g'; ...};
writing
clrs=colormap(something);
didn't qualify as "manual" in my thought process while drafting the query.
hold all, however, does precisely the trick -- a case as noted above where familiarity bred contempt and didn't refresh my memory by perusing the doc's carefully as I always preach here--mea culpa.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2014년 10월 10일
See my ColorOrder demo to let you set up the default colors in advance so that you don't have to specify them every time.
  댓글 수: 1
dpb
dpb 2014년 10월 10일
Thanks, IA, I'll save that code fragment for future...dpb

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


Stephen23
Stephen23 2014년 10월 10일
편집: Stephen23 2014년 10월 10일
Actually this is easy using the property 'DefaultAxesColorOrder', and it doesn't require setting the color in each plot call inside the loop:
N = 6;
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)n*sin(x+2*n*pi/N), X.', 1:N);
set(0,'DefaultAxesColorOrder',summer(N))
for n = 1:N
plot(X(:),Y(:,n), 'linewidth',4);
hold all
end
Although using matrices is more compact:
axes('ColorOrder',summer(N),'NextPlot','replacechildren')
plot(X,Y, 'linewidth',4)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by