Thread Subject: Repeat graph, changing colors.

Subject: Repeat graph, changing colors.

From: Derek Berwald

Date: 25 Apr, 2003 12:59:50

Message: 1 of 8

When I'm trying something out, I often want to change
a parameter, re-run the script, and see how a plot of
the data changes. It's nice if the old data and new
data are both on the plot, and even nicer if they are
in different colors. That's super easy to do if I create
all data with different parameters first, and then just
use plot, but that's not the way I work, usually. I tend to
prefer a more interactive mode when I'm exploring equations.

As an example, here's what I'm thinking of. I have a simple
little m-file that makes some data and plots it:

power=2;
x=[0:10];
y=x.^power;
figure(1)
plot(y)
hold on;

I'll look at the plot, and think "That's interesting, what if power = 3?
So I'll change the code in the editor, and re-run. Works ok, but the
new line on the plot is the default color. I would like each line
added to the plot to be a different color, so they can be easily
distinguished.

Obviously this is not a huge problem, and it's really only important
if I'm experimenting on something like changing parameters of
simulations. If someone can tell me a simple way to do this, I
would appreciate it.

Thanks.

db

******
Derek Berwald
derek@primal.ucdavis.edu

Subject: Repeat graph, changing colors.

From: us

Date: 25 Apr, 2003 16:11:37

Message: 2 of 8

this is one solution:


% create a colormap/colororder, say
     cm=hot(10);
     nl=1;
% plot your data
     line(1:10,rand(1,10),...
          'color',cm(nl,:),...
          'tag','myline');
% change parameters and so on ...
% (here we simply use another
% <rand> data set)
     nl=findall(gcf,'tag','myline');
     nl=nl+1;
     line(1:10,rand(1,10),...
          'color',cm(nl,:),...
          'tag','myline');


... i think you get the idea
us


Derek Berwald wrote:
> I would like each line
> added to the plot to be a different color, so they can be easily
> distinguished.

Subject: Repeat graph, changing colors.

From: us

Date: 25 Apr, 2003 16:13:21

Message: 3 of 8

us wrote:
> nl=findall(gcf,'tag','myline');
> nl=nl+1;


i'm sorry for this stupid typo:


     nl=length(findall(gcf,...));


us

Subject: Repeat graph, changing colors.

From: Derek Berwald

Date: 25 Apr, 2003 14:04:52

Message: 4 of 8

Thanks for your help. This is an even simpler way:

plotcol=['r','m','c','r','g','b'];

nl=nl+1;
power=2;
x=[rand(1,10)];
figure(1)
plot(x,plotcol(nl))
hold on;

The only problem is that I have to define nl=0 before running
the program, and I only can do it six times before running
out of colors. I should be able to loop through the colors by
using REM to create the index, though.

Thanks again.

db

******
Derek Berwald
derek@primal.ucdavis.edu

On Fri, 25 Apr 2003, us wrote:

> us wrote:
> > nl=findall(gcf,'tag','myline');
> > nl=nl+1;
>
>
> i'm sorry for this stupid typo:
>
>
> nl=length(findall(gcf,...));
>
>
> us
>

Subject: Repeat graph, changing colors.

From: us

Date: 25 Apr, 2003 17:21:13

Message: 5 of 8

Derek Berwald wrote:
> This is an even simpler way:
>
> plotcol=['r','m','c','r','g','b'];
> plot(x,plotcol(nl))


> I should be able to loop through the colors by
> using REM to create the index, though.


well, <plotcol> is just another colormap.
hence, if you are never (!) chaning figures/axes in between trials
(that's why i introduced the <findall> bit), you could do:


     plotcol=['r','m','c','r','g','b'];
% or any other colormap, say
% plotcol=bone(6);


     cl=length(plotcol);
     ix=0;
while 1
     ix=rem(ix+1,cl)+1;
     plot(x,'color',plotcol(ix));
% do stuff, eg, leave the loop
     pause;
end


us
     plot(x,y,'color',

Subject: Repeat graph, changing colors.

From: us

Date: 25 Apr, 2003 17:57:23

Message: 6 of 8

us wrote:
> ix=rem(ix+1,cl)+1;


bewitched ...
     ix=rem(ix-1,cl)+1;
... of course
us

Subject: Repeat graph, changing colors.

From: Cristina Vasco

Date: 7 Feb, 2012 16:26:10

Message: 7 of 8

Derek Berwald <derek@primal.ucdavis.edu> wrote in message <Pine.OSF.4.52.0304251242550.7446@chet.ucdavis.edu>...
> When I'm trying something out, I often want to change
> a parameter, re-run the script, and see how a plot of
> the data changes. It's nice if the old data and new
> data are both on the plot, and even nicer if they are
> in different colors. That's super easy to do if I create
> all data with different parameters first, and then just
> use plot, but that's not the way I work, usually. I tend to
> prefer a more interactive mode when I'm exploring equations.
>
> As an example, here's what I'm thinking of. I have a simple
> little m-file that makes some data and plots it:
>
> power=2;
> x=[0:10];
> y=x.^power;
> figure(1)
> plot(y)
> hold on;
>
> I'll look at the plot, and think "That's interesting, what if power = 3?
> So I'll change the code in the editor, and re-run. Works ok, but the
> new line on the plot is the default color. I would like each line
> added to the plot to be a different color, so they can be easily
> distinguished.
>
> Obviously this is not a huge problem, and it's really only important
> if I'm experimenting on something like changing parameters of
> simulations. If someone can tell me a simple way to do this, I
> would appreciate it.
>
> Thanks.
>
> db
>
> ******
> Derek Berwald
> derek@primal.ucdavis.edu


This is a very old post, but there is a simple solution that I didn't see posted here. Change the last line

hold on;

to

hold all;

I'm not sure if this is a new feature but works in MATLAB R2010a. As described in doc hold:

"hold all holds the plot and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and LineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and linestyles from where the last plot stopped in the list."

Cheers,

- C

Subject: Repeat graph, changing colors.

From: Steven_Lord

Date: 7 Feb, 2012 18:06:35

Message: 8 of 8



"Cristina Vasco" <cvasco@gmail.com> wrote in message
news:jgrjb2$ib3$1@newscl01ah.mathworks.com...
> Derek Berwald <derek@primal.ucdavis.edu> wrote in message
> <Pine.OSF.4.52.0304251242550.7446@chet.ucdavis.edu>...

*snip*

> This is a very old post, but there is a simple solution that I didn't see
> posted here. Change the last line
>
> hold on;
>
> to
> hold all;
>
> I'm not sure if this is a new feature but works in MATLAB R2010a. As
> described in doc hold:
>
> "hold all holds the plot and the current line color and line style so that
> subsequent plotting commands do not reset the ColorOrder and
> LineStyleOrder property values to the beginning of the list. Plotting
> commands continue cycling through the predefined colors and linestyles
> from where the last plot stopped in the list."

HOLD ALL was introduced in MATLAB 7.0 (release R14.)

http://www.mathworks.com/help/techdoc/rn/f5-998197.html#f5-1010148

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com