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

Wanderer wanderer at dialup4less.com
Wed Jun 15 12:57:46 EDT 2011


On Jun 15, 12:00 pm, Ravikanth <vvnrk.vanapa... at gmail.com> wrote:
> On Jun 15, 10:32 am, Wanderer <wande... at dialup4less.com> wrote:
>
>
>
>
>
>
>
>
>
> > On Jun 15, 11:04 am, Ravikanth <vvnrk.vanapa... at gmail.com> wrote:
>
> > > 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...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

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.





More information about the Python-list mailing list