MathGroup Archive 1996

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

Search the Archive

Re: extracting points plotted by ContourPlot

  • Subject: [mg3382] Re: extracting points plotted by ContourPlot
  • From: villegas (Robert Villegas)
  • Date: 3 Mar 1996 14:46:22 -0600
  • Approved: usenet@wri.com
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: Wolfram Research, Inc.
  • Sender: daemon at wri.com

In article <4h3l3v$1od at dragonfly.wolfram.com> easther at cfi.waseda.ac.jp  
(Richard Easther) writes:

> Can someone help me with the following problem. After doing a     
> 
>        figure =  ContourPlot[f[x,y], ..., Contours ->{0}], 
> 
> I want to extract the set of points actually plotted - ie a set of {x,y} 
> where f[x,y] == 0 for some additional processing.
> 
> The command TextForm[InputForm[Graphics[ figure ]]] gives me a text 
> string which has a series of Line commands that actually contains the 
> {x,y} values that are plotted.
> 
> Now what I want to know is this: can someone tell me a nice (or even 
> ugly but workable!) way of extracting the list(s) of points plotted by 
> the Line command(s)?


You're most of the way there with Graphics[figure].  That contains the
Line objects, as you noted, so all that's left to do is extract them:

contourLines = Cases[Graphics[figure], _Line, -1]

This returns a list of Line objects that comprise the level curve
z = 0.  There might be more than one if the contour is disconnected
(at least, disconnected in the rectangle in which f was originally
plotted), or none.

And if you want a different level curve z = z0 later on, just Show
'figure' again with a different Contours setting.

contourLines =
  Cases[Show[figure, Contours->{z0}, DisplayFunction->Graphics,
    ContourShading->False], _Line, -1]

DisplayFunction->Graphics causes the new contour plot to be converted
straight to Graphics without being displayed, and ContourShading->False
causes the shaded Polygon's to be left out of the Graphics object,
since they are baggage unimportant to calculating the contour lines.  If
you actually do want a new graph displayed for every new contour
computed, use Graphics @ Show[figure, Contours->{z0}] as the first
argument of Cases instead.


  Here is a general function LevelCurve that returns a rule
z -> {Line, ...} for each contour you ask for.

ExtractLines[cgraph_ContourGraphics] :=
  Cases[Show[cgraph, ContourShading->False, DisplayFunction->Graphics],
    _Line, -1]

LevelCurve[cgraph_ContourGraphics, z_List] :=

LevelCurve[cgraph_ContourGraphics, z_] :=
  First @ LevelCurve[cgraph, {z}]



Example:

In[5]:= cplot = ContourPlot[Arg @ AiryAi[x + y I], {x, 3, 5}, {y, -4, -2},
  PlotPoints->40]

Out[5]= -ContourGraphics-

In[6]:= LevelCurve[cplot, 5/6 Pi]

        5 Pi
Out[6]= ---- -> 
         6
 
>    {Line[{{5., -3.82523}, {4.94872, -3.84198}, {4.93604, -3.84615}, 
 
>       {4.89744, -3.85893}, {4.84615, -3.87607}, {4.79487, -3.89341}, 
 
>       {4.78304, -3.89744}, {4.74359, -3.91095}, {4.69231, -3.92869}, 
 
>       {4.64103, -3.94665}, {4.63515, -3.94872}, {4.58974, -3.96481}, 
 
>       {4.53846, -3.98319}, {4.49209, -4.}}]}


Robby Villegas


  • Prev by Date: using mathematica for computer simulation?
  • Next by Date: Mathematica on NT 4.0 SUR?
  • Previous by thread: Re: extracting points plotted by ContourPlot
  • Next by thread: Re: Constant term in polynomial?