Automating line markers, colors...
조회 수: 8 (최근 30일)
이전 댓글 표시
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...
댓글 수: 0
채택된 답변
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
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.
추가 답변 (2개)
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.
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!