MathGroup Archive 1998

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

Search the Archive

Re: Shading Functions

  • To: mathgroup at
  • Subject: [mg14347] Re: Shading Functions
  • From: "Allan Hayes" <hay at>
  • Date: Thu, 15 Oct 1998 00:28:50 -0400
  • References: <6vuo01$>
  • Sender: owner-wri-mathgroup at

Des Penny wrote in message <6vuo01$899 at>...
>Hi Folks:
>I'm having trouble invoking the shading function in one form.
>The following works ok:
>p1=Plot3D[Sin[x y],{x,-1,1},{y,-2,2}, AxesLabel->{X,Y,Z}];
>If we map the range of the plot into {0,0.8}, we will now be able to
>color the heights of a plot so that the lowest points are red and the
>highest points are Blue/Purple.
>The max and min values of Sin are {-1,1}, so this checks.
>We now want to map the heights, z, into the range {0,0.8}. Using the
>linear map, c=a z+b we find c=0.40 z+0.40 will work.
>(0.4 zrange+0.4)
>If we now operate on each z value with Hue we will display this z value
>in the correct color:
>p2=Plot3D[{Sin[x y], colorFun[Sin[x y]]},{x,-1,1},{y,-2,2},
>This works fine.
>However the following does not work correctly.  I'm not sure why.
>Show[p2, ColorFunction->colorFun];
>I was under the impression that the above statement caused colorFun to
>operate on the z values of the function.  We should get the same plot
>as the preceeding Plot3D but we don't.  The output of the above clearly
>shows that colorFun is operating, but not correctly.
>Can anyone help explain what's going on?
>Des Penny


The mechanisms are different.
Here is an attempt at describing them.

1) With Plot3D[{f[x, y],c[x,y]},{x,xmin,xmax},{y,ymin,ymax}]

For each polygon displayed, the color is given by the "average" of
c[x,y] over the x y 's  for the vertices What does "average" mean? The
following seems to work , where  av means average over the polygon
vertex x y's) , but cav also takes into account the cycling of
Hue[x](for example Hue[0] and Hue[1] both give red) in a way that I
have not yet sorted out completely.

    average RGB[r,g,b]  = RGBColor[ av[r], av[g], av[b]]
    average Hue[h, s,b] = RGBColor[ cav[r], av[g], av[b]]

The colors are stored in an extra grid in the SurfaceGraphics object

2) With Plot3D[f[x y],{x,xmin,xmax},{y,ymin,ymax}, ColorFunction ->cf]

Each polygon is shaded by the color directive cf[average scaled height
at vertices],
Where, with PlotRange  = {{xmin,xmax},{ ymin,ymax}, {zmin,zmax}, the
scaling of the heights is linear with zmin ->0 and zmax ->1.

An extra twist is that {zmin, zmax} will not usually be {Min[f{x,y} at
plot points],
Max[f{x,y} at plot points]}.

3) Your example

In place of your In[7]

Show[p1, ColorFunction->(Hue[.8#]&)]

will give a result close to the result from p2.


Allan Hayes
Mathematica Training and Consulting
hay at
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

  • Prev by Date: Recommended books for Mathematica
  • Next by Date: Re: Mathmatica Problem...
  • Previous by thread: Re: Shading Functions
  • Next by thread: Re: Shading Functions