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 -