MathGroup Archive 1994

[Date Index] [Thread Index] [Author Index]

Search the Archive

re: Coloring 2DPlot with different colors

  • To: mathgroup at
  • Subject: re: Coloring 2DPlot with different colors
  • From: Paul=Rubin%Management%Business at
  • Date: Mon, 13 Jun 94 11:02:10 EDT

Wang Jian-hua <WANGJH at> wrote:

>Does any one knows how to make on 2DPlot with different colors? For
>example, I'd like to use two different color to emphasize decreasing or
>increasing of a function or the positive and negative values of a function.

>Thanks for help.


There are at least two ways (the not-so-easy way and the not-even-that-easy 
way).  The easier of the two, when you are going to use a small number of 
distinct colors, is to split the function you want to plot into several 
functions, each taking values on a different part of the domain.  For 
example, to plot the Sin function with different colors for positive and 
negative values:

In[]:=  f1[x_] := Sin[x] /; Sin[x] >= 0
In[]:=  f2[x_] := Sin[x] /; Sin[x] < 0

Function f1 fails to reduce to a number if the value of Sin[x] is negative.

In[]:=  Off[ Plot::plnr ];
In[]:=  Plot[ {f1[x], f2[x]}, {x, 0, 6 Pi},
              PlotStyle -> {{Hue[ .3 ]}, {Hue[ .7 ]}} ];
In[]:=  On[ Plot::plnr ];

For each value of x in Plot, either f1 or f2 fails to yield a number, so no 
point is plotted for that function.  I turned off the warning messages that 
produces.  I used Hue, but RGBColor, CYMKColor or GrayLevel would work just 
as well.

To get closer to continuous coloring takes a lot more work.  Here is the Sin 
function plotted with the Hue at each point equal to the absolute value of 
the ordinate:

In[]:=  p = Plot[ Sin[x], {x, 0, 6 Pi},
                  DisplayFunction -> Identity ];
(* Store the plot in p but don't display it *)
In[]:=  q = First[ First[ Cases[ p, Line[_], 20 ] ] ];
(* Isolate the argument of the Line[] command in q *)
In[]:=  q // Short
Out[]=  {{0., 0.}, <<159>>, {18.8496, -7.34764 10   }}
(* q is a list of 161 points *)
In[]:=  q1 = Partition[ q, 2, 1 ];
(* Partition the point list into a list of line segments *)
In[]:=  q1 // Short
Out[]=  {{{0., 0.}, {0.392699, 0.382683}}, <<159>>}
(* q1 has 160 pairs of points *)
In[]:=  q2 = {Hue[ Abs[ #[[1, 2]] ] ], Line[ # ]}& /@ q1;
(* Replace each pair of points with a pair of graphics primitives, Hue
   and Line, where the argument for Hue is the ordinate of the left
   endpoint of the line segment *)
In[]:=  q2 // Short
Out[]=  {{Hue[0.], Line[{{0., 0.}, <<1>>}]}, <<159>>}
In[]:=  p1 = p /. {Line[_]} -> q2;
(* Replace the line in the original plot with this list *)
In[]:=  Show[ p1, DisplayFunction -> $DisplayFunction ]
(* Display the result *)

* Paul A. Rubin                                  Phone: (517) 336-3509   *
* Department of Management                       Fax:   (517) 336-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE

  • Prev by Date: Re: Scale Transformations
  • Next by Date: MW - Education Today - Reviews
  • Previous by thread: Re:Coloring 2DPlot with different colors
  • Next by thread: Coloring 2DPlot with different colors