Re: Extracting data points from Plot[]
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg797] Re: Extracting data points from Plot[]
- From: villegas (Robert Villegas)
- Date: Wed, 19 Apr 1995 00:48:34 -0400
- Organization: Wolfram Research, Inc.
I just noticed something from Jon Guyer's post, which deserves a couple comments. He was plotting several functions, not just one, and I had the plotting of a single function in mind in my last post. [his example] > Plot[{x,x^2},{x,0,2}]; Something that might not be immediately obvious (I didn't realize it until someone sent in a question a while back) is that if you give Plot a list of functions, it samples them separately, in order. They may behave very differently on the domain, and require completely different distributions of sample points to be graphed smoothly. A quick way to show this is to make two functions that store samples in different lists when they are evaluated, and then look at the lists afterwards. I set PlotPoints and PlotDivision low, so that not many samples would be generated and the lists wouldn't take up much space on the page. In[1]:= f[x_] := (AppendTo[fList, x]; x) In[2]:= g[x_] := (AppendTo[gList, x]; x^2) In[3]:= fList = gList = {} Out[3]= {} In[4]:= Plot[{f[x], g[x]}, {x, -3, 3}, PlotPoints->5, PlotDivision->5] Out[4]= -Graphics- In[5]:= fList Out[5]= {-3., -1.5, 0., 1.5, 3.} (* x^2 is a more active function than x is, so it needs more sampling: *) In[6]:= gList Out[6]= {-3., -1.5, 0., -2.25, -0.75, -1.875, -1.125, -0.375, -0.9375, > -0.5625, 1.5, 0.75, 0.375, -0.1875, 0.1875, 1.125, 0.5625, 3., 2.25, > 1.875} So this is one important thing to know if you're using the AppendTo trick to store samples. Also, this affects the way I used Cases to extract the Line objects out of the Graphics expression. If you're plotting several functions, not just one like I was assuming, you need to modify that method. Here's an example of how to do it (the only difference is the use of Map): In[4]:= p = Plot[{x, x^2, x^3}, {x, 0, 1}] Out[4]= -Graphics- In[5]:= data = First[p]; In[6]:= {curve1, curve2, curve3} = Map[Cases[#, Line[pts_] :> pts, -1]&, data]; The first part of the Graphics expression, which I called 'data', still contains all the Line objects you want. The first element of 'data' will contain the Line's (possibly just one) for the first function, the second element of 'data' will contain the Line's for the second function, and so on. This keeps the curves separate, and knowing this, you can extract separate lists of points. I'm sorry that my first post probably caused confusion for people trying to use the Cases method directly on a plot of several functions. Robby Villegas