0

I simply have a loop with a few functions that I want to plot with different colors. Should it not just be something like this:

 colors = 'rgbm';
 i=0;
 for p=.1:.1:.6;   
    k = ezplot(subs(J,q,p),[0,3]);        
    set(k, 'Color', colors(i));
    i = i+1;
 end

This is more preudo-code which does not work. How do I do this? Thanks!

Dan
  • 45,079
  • 17
  • 88
  • 157
tw-S
  • 579
  • 1
  • 5
  • 23

2 Answers2

2

Begin with i=1, not 0.

And add hold onat the beginnig, otherwise you only see the last plot

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    And if you use `hold all` instead of hold on then you don't even need the `set(...` line, the colours will change automatically. – Dan Jul 23 '13 at 15:05
  • 1
    You can also use one of the following File Exchange submissions to get more colours: - http://www.mathworks.co.uk/matlabcentral/fileexchange/29702-generate-maximally-perceptually-distinct-colors - http://www.mathworks.co.uk/matlabcentral/fileexchange/42673-beautiful-and-distinguishable-line-colors-+-colormap – am304 Jul 23 '13 at 15:05
  • @Dan Hey, that's a good one! – Luis Mendo Jul 23 '13 at 15:15
  • @Dan: Nice, but then we don't know which color belongs to which plot, rigth? – tw-S Jul 24 '13 at 08:20
  • @tw-S well you can add a legend with 1 line of code... also they go in order. But ya, if you want full control then no. – Dan Jul 24 '13 at 08:28
  • @Dan: Thanks! How do you generate a legend in the loop above (see original post)? The line text in the legend should just be the parameter value of p (i.e., .1,.2,.3,...). – tw-S Jul 24 '13 at 08:47
  • @tw-S http://stackoverflow.com/questions/16236421/how-can-i-change-the-color-of-the-plot-in-each-iteration-in-matlab/16236580#16236580 – Dan Jul 24 '13 at 09:01
  • 1
    @tw-S if this answer has been helpful to you (which I'm assuming it has since you've accepted it), you should also consider giving it an upvote by clicking on the upward pointing arrow just to the left of the question. – Dan Jul 24 '13 at 09:03
1

if you want to plot with different colours, you can create and use a so called colormap, e.g. like this:

colors=lines(12);

and later on:

colors(index)

there are other maps like "jet":

Mathworks-Documentation for colormap

Lucius II.
  • 1,832
  • 12
  • 19