creating a multi colored graph with respect to the values in y-axis

R

Ravikanth

Hi all,

I am a beginner in python. I need to implement a graph with multiple
colors in it.
In a way, I have a function which varies with respect to time and
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when the values of
amplitude are in a particular range say 1-3, the color of graph should
be red.
If the amplitude is in the range from 3-5 the graph need to be in
color blue etc..,

Can somebody guide me on this, how to achive this functionality.

Regards,
Ravikanth
 
W

Wanderer

Hi all,

I am a beginner in python. I need to implement a graph with multiple
colors in it.
In a way, I have a function which varies with respect to time and
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when the values of
amplitude are in a particular range say 1-3, the color of graph should
be red.
If the amplitude is in the range from 3-5 the graph need to be in
color blue etc..,

Can somebody guide me on this, how to achive this functionality.

Regards,
Ravikanth

Check out the examples in matplotlib.

http://matplotlib.sourceforge.net/examples/pylab_examples/multicolored_line..html
 
R

Ravikanth

Check out the examples in matplotlib.

http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...-Hide quoted text -

- Show quoted text -

I did go through the side wanderer.

I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0) (1.0, 0.0, 0.0, 1.0)
cmap(1) (0.0, 0.5, 0.0, 1.0)
cmap(2) (0.0, 0.0, 1.0, 1.0)
cmap(3)
(0.0, 0.0, 1.0, 1.0)

In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ? what is the
exact use of linecollection.set_array(z) in this context.

Thanks,
Ravikanth
 
W

Wanderer

Check out the examples in matplotlib.
- Show quoted text -

I did go through the side wanderer.

I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,

cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)

In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)

(1.0, 0.0, 0.0, 1.0)>>> cmap(1)

(0.0, 0.5, 0.0, 1.0)>>> cmap(2)

(0.0, 0.0, 1.0, 1.0)>>> cmap(3)

(0.0, 0.0, 1.0, 1.0)

In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what is the
exact use of linecollection.set_array(z) in this context.

Thanks,
Ravikanth

The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand. Here are the steps.

1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
(Why they add the trailing zero in 0.50 and don't change 1 to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.
 
R

Ravikanth

I did go through the side wanderer.
I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)
(1.0, 0.0, 0.0, 1.0)>>> cmap(1)
(0.0, 0.5, 0.0, 1.0)>>> cmap(2)
(0.0, 0.0, 1.0, 1.0)>>> cmap(3)
(0.0, 0.0, 1.0, 1.0)
In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what is the
exact use of linecollection.set_array(z) in this context.
Thanks,
Ravikanth

The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand.  Here are the steps.

1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
   (Why they add the trailing zero in 0.50 and don't change 1 to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.- Hide quoted text -

- Show quoted text -

Hi Wanderer,

Thanks for your help. It works now.
Just wanted to know how to go about when I have to do my color mapping
not only with respect to range of values on y-axis but also based on
some other conditions as well. i.e, say ( range && <condtion1> &&
<condition2> ) where condition1 could be occurance of some event say,
a flag1 is set true and condition2 may be another flag2 set to false.
Just wanted to use my color mapping not only based on boundaries but
also on occurance of other events as well.
In this context do i have to modify the source of BoundaryNorm in
matplotlib function...?? Can you give me some insights into this.

Regards,
Ravikanth
 
W

Wanderer

Hi all,
I am a beginner in python. I need to implement a graph with multiple
colors in it.
In a way, I have a function which varies with respect to time and
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when the values of
amplitude are in a particular range say 1-3, the color of graph should
be red.
If the amplitude is in the range from 3-5 the graph need to be in
color blue etc..,
Can somebody guide me on this, how to achive this functionality.
Regards,
Ravikanth
Check out the examples in matplotlib.
http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...text -
- Show quoted text -
I did go through the side wanderer.
I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)
(1.0, 0.0, 0.0, 1.0)>>> cmap(1)
(0.0, 0.5, 0.0, 1.0)>>> cmap(2)
(0.0, 0.0, 1.0, 1.0)>>> cmap(3)
(0.0, 0.0, 1.0, 1.0)
In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what is the
exact use of linecollection.set_array(z) in this context.
Thanks,
Ravikanth
The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand.  Here are the steps.
1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
   (Why they add the trailing zero in 0.50 and don't change 1 to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.- Hide quoted text -
- Show quoted text -

Hi Wanderer,

Thanks for your help. It works now.
Just wanted to know how to go about when I have to do my color mapping
not only with respect to range of values on y-axis but also based on
some other conditions as well. i.e, say ( range && <condtion1> &&
<condition2> ) where condition1 could be occurance of some event say,
a flag1 is set true and condition2 may be another flag2 set to false.
Just wanted to use my color mapping not only based on boundaries but
also on occurance of other events as well.
In this context do i have to modify the source of BoundaryNorm in
matplotlib function...?? Can you give me some insights into this.

Regards,
Ravikanth

I don't know if there is another way, but I think changing the
lc.set_array input would be the easiest. Each point has an (x,y,z)
where z determines the color by what range it is in. You would use
your conditions to set points in z to the desired color.
 
R

Ravikanth

Hi all,
I am a beginner in python. I need to implement a graph with multiple
colors in it.
In a way, I have a function which varies with respect to time and
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when the values of
amplitude are in a particular range say 1-3, the color of graphshould
be red.
If the amplitude is in the range from 3-5 the graph need to be in
color blue etc..,
Can somebody guide me on this, how to achive this functionality..
Regards,
Ravikanth
Check out the examples in matplotlib.
http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...-
- Show quoted text -
I did go through the side wanderer.
I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)
(1.0, 0.0, 0.0, 1.0)>>> cmap(1)
(0.0, 0.5, 0.0, 1.0)>>> cmap(2)
(0.0, 0.0, 1.0, 1.0)>>> cmap(3)
(0.0, 0.0, 1.0, 1.0)
In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what is the
exact use of linecollection.set_array(z) in this context.
Thanks,
Ravikanth
The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand.  Here are the steps.
1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
   (Why they add the trailing zero in 0.50 and don't change 1 to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.- Hide quoted text -
- Show quoted text -
Hi Wanderer,
Thanks for your help. It works now.
Just wanted to know how to go about when I have to do my color mapping
not only with respect to range of values on y-axis but also based on
some other conditions as well. i.e, say ( range && <condtion1> &&
<condition2> ) where condition1 could be occurance of some event say,
a flag1 is set true and condition2 may be another flag2 set to false.
Just wanted to use my color mapping not only based on boundaries but
also on occurance of other events as well.
In this context do i have to modify the source of BoundaryNorm in
matplotlib function...?? Can you give me some insights into this.
Regards,
Ravikanth

I don't know if there is another way, but I think changing the
lc.set_array input would be the easiest. Each point has an (x,y,z)
where z determines the color by what range it is in. You would use
your conditions to set points in z to the desired color.- Hide quoted text -

- Show quoted text -

does (x,y,z) mean adding another dimension 'z' (which is to be used as
a condition) to the step 4) as suggested in the steps outlined to me
before ?
 
W

Wanderer

Hi all,
I am a beginner in python. I need to implement a graph with multiple
colors in it.
In a way, I have a function which varies with respect to timeand
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when the values of
amplitude are in a particular range say 1-3, the color of graph should
be red.
If the amplitude is in the range from 3-5 the graph need to be in
color blue etc..,
Can somebody guide me on this, how to achive this functionality.
Regards,
Ravikanth
Check out the examples in matplotlib.
http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...
- Show quoted text -
I did go through the side wanderer.
I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)
(1.0, 0.0, 0.0, 1.0)>>> cmap(1)
(0.0, 0.5, 0.0, 1.0)>>> cmap(2)
(0.0, 0.0, 1.0, 1.0)>>> cmap(3)
(0.0, 0.0, 1.0, 1.0)
In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what is the
exact use of linecollection.set_array(z) in this context.
Thanks,
Ravikanth
The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand.  Here are the steps.
1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
   (Why they add the trailing zero in 0.50 and don't change 1 to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.- Hide quoted text -
- Show quoted text -
Hi Wanderer,
Thanks for your help. It works now.
Just wanted to know how to go about when I have to do my color mapping
not only with respect to range of values on y-axis but also based on
some other conditions as well. i.e, say ( range && <condtion1> &&
<condition2> ) where condition1 could be occurance of some event say,
a flag1 is set true and condition2 may be another flag2 set to false.
Just wanted to use my color mapping not only based on boundaries but
also on occurance of other events as well.
In this context do i have to modify the source of BoundaryNorm in
matplotlib function...?? Can you give me some insights into this.
Regards,
Ravikanth
I don't know if there is another way, but I think changing the
lc.set_array input would be the easiest. Each point has an (x,y,z)
where z determines the color by what range it is in. You would use
your conditions to set points in z to the desired color.- Hide quoted text -
- Show quoted text -

does (x,y,z) mean adding another dimension 'z' (which is to be used as
a condition) to the step 4) as suggested in the steps outlined to me
before ?

In the matplotlib example they x,y and z.

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:])) # first derivative

Where x and y are the coordinates of the plot and z is the constraint
used to color the line. You can modify z with you conditions by adding
code that looks like

for i, xvalue in enumerate(x);
if mycondition(xvalue):
z = 0

lc.set_array(z)

So for values of x that meet your condition, they get the color for 0.
 
R

Ravikanth

Hi all,
I am a beginner in python. I need to implement a graph withmultiple
colors in it.
In a way, I have a function which varies with respect to time and
amplitude. I have time on x-axis and amplitude on y-axis. Lets say the
amplitude of the graph is divided into 4 ranges, say 1-3,3-5,5-9,
10-3. I need to plot the graph in such a way that, when thevalues of
amplitude are in a particular range say 1-3, the color of graph should
be red.
If the amplitude is in the range from 3-5 the graph need tobe in
color blue etc..,
Can somebody guide me on this, how to achive this functionality.
Regards,
Ravikanth
Check out the examples in matplotlib.
http://matplotlib.sourceforge.net/examples/pylab_examples/multicolore...
- Show quoted text -
I did go through the side wanderer.
I wasn't able to figure out the usage of boundaryNorm and
lc.set_array(z) , in that link.
according to my understanding,
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
In the above lines of code, as per my understanding,
Listedcolor map, maps the colors r,g and b to specific indexes into
cmap
i.e cmap(0) represents red,
cmap(1) represents blue
cmap(2) represents green.
for any index greater than 3 a color of blue is returned..
cmap = ListedColormap(['r', 'g', 'b'])
cmap(0)
(1.0, 0.0, 0.0, 1.0)>>> cmap(1)
(0.0, 0.5, 0.0, 1.0)>>> cmap(2)
(0.0, 0.0, 1.0, 1.0)>>> cmap(3)
(0.0, 0.0, 1.0, 1.0)
In this context, I was not able to understand what does boundaryNorm
does.
We have 3 colors and we are using 4 values as argument in boundaryNorm.
[-1, -0.5, 0.5, 1], the comment reads slope of 'z' is being mapped to
the values in boundary norm. How is it handled.
Does the function call " lc.set_array(z) " does it ?  what isthe
exact use of linecollection.set_array(z) in this context.
Thanks,
Ravikanth
The colors are referring to the slope of the line. Change
'lc.set_array(z)' to 'lc.set_array(y)' and it might be easier to
understand.  Here are the steps.
1. Define the functions x,y and z,
2. Define the colors 'red', 'green' and 'blue' with ListedColorMap
3. Define the three regions, (-1.0 to -0.50, -0.50 to 0.50, 0.50 to
1.0) with BoundaryNorm([-1,-0.50, 0.50,1], cmap.N).
   (Why they add the trailing zero in 0.50 and don't change 1to 1.0;
I don't know)
4. Create an array of (x,y) points.
5. Create a collection of tiny segments [(x1,y1),(x2,y2)] and color
them with cmap using the boundary rules of norm. lc =
LineCollection(segments, cmap=cmap, norm=norm)
6. Use lc.set_array(y) to determine how to color the segments.
7. Plot it.- Hide quoted text -
- Show quoted text -
Hi Wanderer,
Thanks for your help. It works now.
Just wanted to know how to go about when I have to do my color mapping
not only with respect to range of values on y-axis but also based on
some other conditions as well. i.e, say ( range && <condtion1> &&
<condition2> ) where condition1 could be occurance of some event say,
a flag1 is set true and condition2 may be another flag2 set to false.
Just wanted to use my color mapping not only based on boundaries but
also on occurance of other events as well.
In this context do i have to modify the source of BoundaryNorm in
matplotlib function...?? Can you give me some insights into this.
Regards,
Ravikanth
I don't know if there is another way, but I think changing the
lc.set_array input would be the easiest. Each point has an (x,y,z)
where z determines the color by what range it is in. You would use
your conditions to set points in z to the desired color.- Hide quotedtext -
- Show quoted text -
does (x,y,z) mean adding another dimension 'z' (which is to be used as
a condition) to the step 4) as suggested in the steps outlined to me
before ?

In the matplotlib example they x,y and z.

x = np.linspace(0, 3 * np.pi, 500)
y = np.sin(x)
z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative

Where x and y are the coordinates of the plot and z is the constraint
used to color the line. You can modify z with you conditions by adding
code that looks like

for i, xvalue in enumerate(x);
    if mycondition(xvalue):
        z = 0

lc.set_array(z)

So for values of x that meet your condition, they get the color for 0.- Hide quoted text -

- Show quoted text -


I've implemented as u suggested and it all works!!
Thank you Wanderer for guiding me on this.
I really appreciate all your help.

--Ravikanth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top