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!