MathGroup Archive 2002

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

Search the Archive

RE: Gridlines in contour Plots

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38491] RE: [mg38473] Gridlines in contour Plots
  • From: "David Park" <djmp at earthlink.net>
  • Date: Fri, 20 Dec 2002 04:24:51 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Ross,

That is an interesting problem and a useful solution. I am going to repeat
your routine here because there was an extra line break that needed to be
fixed when I copied it. Then I will give an example.

gridlinestogo[{xlow_, xhigh_}, {ylow_, yhigh_},
   color_:RGBColor[0., 0., 0.5]] := Module[{o, g, r},
   o = AbsoluteOptions[Plot[(ylow*xhigh - yhigh*xlow)/
         (xhigh - xlow) + ((yhigh - ylow)*x)/(xhigh - xlow),
       {x, xlow, xhigh}, Frame -> True, GridLines -> Automatic,
       DisplayFunction -> Identity]]; g = GridLines /. o;
    g = g /. RGBColor[0., 0., 0.5] -> color;
    r = PlotRange /. o;
    Join[({Sequence @@ #1[[2]], Line[{{#1[[1]], r[[2,1]]},
          {#1[[1]], r[[2,2]]}}]} & ) /@ g[[1]],
     ({Sequence @@ #1[[2]], Line[{{r[[1,1]], #1[[1]]},
          {r[[1,2]], #1[[1]]}}]} & ) /@ g[[2]]]]

Example...

ContourPlot[Sin[x*y], {x, -(Pi/2), Pi/2}, {y, -(Pi/2), Pi/2},
   ColorFunction -> Hue,
   Contours -> 10,
   PlotPoints -> 50,
   Epilog -> gridlinestogo[{-(Pi/2), Pi/2}, {-(Pi/2), Pi/2},
     GrayLevel[0]]];

The following more straightforward solution doesn't work because ContourPlot
won't accept GridLines as an option.

gridlines = Table[{x, GrayLevel[1]}, {x, -Pi/2, Pi/2, Pi/8}];

ContourPlot[Sin[x*y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
   ColorFunction -> Hue,
   Contours -> 10,
   PlotPoints -> 25,
   GridLines -> {gridlines, gridlines}];

We could get around this by the following code. First we have to load the
FilledPlot package. (Every night I pray for world peace and that WRI will
see fit to make AxesFront a general Graphics option instead of burying it in
FilledPlot - not necessarily in that order!)
1) Make the ContourPlot and convert it from ContourGraphics to Graphics.
2) Suppress the extra plot with DisplayFunction -> Identity.
3) Show the resulting plot with GridLines -> Automatic.
4) Turn the DisplayFunction back on.
5) Use AxesFront to put the grid lines in front.

Needs["Graphics`FilledPlot`"]

Show[Graphics[
      ContourPlot[Sin[x*y], {x, -Pi/2, Pi/2}, {y, -Pi/2, Pi/2},
        ColorFunction -> Hue, Contours -> 10, PlotPoints -> 25,
        DisplayFunction -> Identity]],
    GridLines -> Automatic,
    DisplayFunction -> $DisplayFunction,
    AxesFront -> True];


With the DrawGraphics package from my web site an easier and more intuitive
approach can be taken. First, the FilledPlot package is automatically loaded
whenever AxesFront is used. The ContourDraw statement is just like
ContourPlot except that it converts to Graphics and extracts the primitive
graphics. The side plot is also automatically eliminated. The only slight
disadvantage is that the overall plot options such as Frame and AspectRatio
are thrown away by ContourDraw and must be given in the Draw2D statement.
The big advantage is that you could add any other graphical objects such as
plotted curves, lines etc., just by listing them after ContourDraw.

Draw2D[
{ContourDraw[Sin[x*y], {x, -Pi/2, Pi/2},
     {y, -Pi/2, Pi/2},
     ColorFunction -> Hue,
     Contours -> 10,
     PlotPoints -> 25]},
   AspectRatio -> Automatic,
   Frame -> True,
   GridLines -> Automatic,
   AxesFront -> True];

DrawGraphics also has CustomTicks and CustomGridLines routines. Here the
ticks and grid lines run in multiples of Pi/8.

Draw2D[
 {ContourDraw[Sin[x*y], {x, -(Pi/2), Pi/2}, {y, -(Pi/2), Pi/2},
  ColorFunction -> Hue,
  Contours -> 10,
  PlotPoints -> 25]},
 AspectRatio -> Automatic,
 Frame -> True,
 FrameTicks ->
  {CustomTicks[Identity, {-Pi/2, Pi/2, Pi/8, 1}],
   CustomTicks[Identity, {-Pi/2, Pi/2, Pi/8, 1}], None, None},
 GridLines ->
 {CustomGridLines[Identity, {-(Pi/2), Pi/2, Pi/8}, {White}],
  CustomGridLines[Identity, {-(Pi/2), Pi/2, Pi/8}, {White}]},
 AxesFront -> True,
 ImageSize -> 450];

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Ross Sean Civ AFRL/DELO [mailto:Sean.Ross at kirtland.af.mil]
To: mathgroup at smc.vnet.net

I noticed that there are no Gridlines option in Contour graphics.  I am kind
of partial to Gridlines because it makes graphs a lot easier to read and get
numbers from in hard copy.  I decided to piggyback off of the automatic
gridlines option in other 2-D graphics.   I also added an optional color
that the normal Gridlines option doesn't have.     To use it, just put


Epilog->gridlinestogo[{xlow,xhigh},{ylow,yhigh},optionalcolor] in your
contour graphics plot.



Clear[gridlinestogo];

gridlinestogo[{xlow_,xhigh_},{ylow_,yhigh_},color_:RGBColor[0.,0.,0.5]]:=Mod
ule[{o,g,r},

o=AbsoluteOptions[
Plot[ (ylow xhigh - yhigh xlow )/(xhigh-xlow)+
(yhigh-ylow)/(xhigh-xlow) x,
{x,xlow,xhigh},
Frame->True,
GridLines->Automatic,
DisplayFunction->Identity]];

g=GridLines/.o;
g=g/.RGBColor[0.,0.,0.5]->color;
r=PlotRange/.o;


Join[
Map[{Sequence@@#[[2]],Line[{{#[[1]],r[[2,1]]},{#[[1]],r[[2,2]]}}]}&,

g[[1]]],

Map[{Sequence@@#[[2]],Line[{{r[[1,1]],#[[1]]},{r[[1,2]],#[[1]]}}]}&,

g[[2]]



]
]
]

Dr. Sean Ross
AFRL/DELO
3550 Aberdeen Ave. SE building 761
Kirtland AFB, NM 87117

email: Sean.Ross at Kirtland.af.mil




  • Prev by Date: Re: Memory Leak with KSubsets?
  • Next by Date: Re: Nonparametric kernel density estimators
  • Previous by thread: Gridlines in contour Plots
  • Next by thread: Problem rendering Mathematica fonts in eps and pdf files.