Understanding the Code: How to Extract Data from a Plot in Mathematica

  • Mathematica
  • Thread starter Swamp Thing
  • Start date
  • Tags
    Data Plot
In summary, the conversation discusses a method for extracting data from a Plot in Mathematica using the Cases function. The code uses a pattern to match a Line with x,y coordinates and then uses the :> operator to strip off the Line wrapper. The last two arguments control the levelspec and number of patterns to match. The conversation also speculates on how someone may have come up with this code, by using the InputForm function to view the data and manipulating the levelspec and pattern matching.
  • #1
Swamp Thing
Insights Author
970
670
An answer posted here...
https://mathematica.stackexchange.com/questions/19859/plot-extract-data-to-a-file
... says you can extract the data from a Plot by doing this:
Code:
data = Cases[Plot[Sin@x, {x, 0, 2 Pi}], Line[data_] :> data, -4, 1][[1]];

Having looked at the doc page on Cases, I can't figure out how this works. (And I can't imagine how someone would come up with this, using only the documentation).

I find that changing the last two arguments (-4 and 1) doesn't affect the result, as long as the third one is negative.

So what is going on in that code?
 
Physics news on Phys.org
  • #2
To try to imagine how someone would come up with this, look at

InputForm[Plot[Sin@x,{x,0,2Pi}]]

InputForm presents the data it is given into a less "pretty printed" version that you can look at and use.

When you look at that you should see there are some bits adjusting the appearance and then there is a Line with a vast number of x,y coordinates. And that is followed by more bits adjusting the appearance.

Cases takes an expression and tries to find a matching pattern in it. So in this case it is looking for that Line[...]

Next the :> is doing a substitution. Really all that is doing is stripping off the Line that is wrapped around the points.

And then the last two numbers look like they are choosing the levelspec and the number of patterns to match.

If you try to compare this description to what you see from the InputForm you can see it is trying to control the extraction of the data points that would default to be turned your graph on the screen.

Does this give you an idea how to think about that code?
 
  • Informative
Likes Swamp Thing

FAQ: Understanding the Code: How to Extract Data from a Plot in Mathematica

What is the purpose of extracting data from a plot?

The purpose of extracting data from a plot is to obtain specific numerical values or information that is represented visually in the plot. This allows for further analysis and interpretation of the data.

How do I extract data from a plot?

Data can be extracted from a plot by using a variety of methods such as manually reading and recording values, using software tools, or using specialized techniques such as digitization.

What are some common tools for extracting data from a plot?

Some common tools for extracting data from a plot include spreadsheet software like Excel, graphing software like Origin, and specialized digitization software like WebPlotDigitizer.

What types of data can be extracted from a plot?

Various types of data can be extracted from a plot, such as numerical values, trends, patterns, and relationships between variables.

Are there any limitations to extracting data from a plot?

Yes, there can be limitations when extracting data from a plot. These can include errors in measurement and digitization, difficulty in obtaining precise data points, and limitations in the software or tools being used.

Similar threads

Replies
4
Views
1K
Replies
1
Views
917
Replies
4
Views
2K
Replies
6
Views
3K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
12
Views
3K
Replies
1
Views
1K
Back
Top