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!