Automating line markers, colors...

24 views (last 30 days)
dpb
dpb on 10 Oct 2014
Commented: dpb on 10 Oct 2014
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...

Accepted Answer

Star Strider
Star Strider on 10 Oct 2014
Edited: Star Strider on 10 Oct 2014
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 Comments
Image Analyst
Image Analyst on 10 Oct 2014
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 on 10 Oct 2014
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.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 10 Oct 2014
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 Comment
dpb
dpb on 10 Oct 2014
Thanks, IA, I'll save that code fragment for future...dpb

Sign in to comment.


Stephen23
Stephen23 on 10 Oct 2014
Edited: Stephen23 on 10 Oct 2014
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)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!