Mathematica:如何获取 plot 命令绘制的数据点? [英] Mathematica: How to obtain data points plotted by plot command?

查看:51
本文介绍了Mathematica:如何获取 plot 命令绘制的数据点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Plot 绘制函数时,我想获取 Plot 命令绘制的一组数据点.

例如,如何获取 Plot 在以下简单示例中使用的点列表 {t,f}?

f = Sin[t]绘图[f, {t, 0, 10}]

我尝试使用一种将值附加到列表的方法,如 Jerry B. Keiper 的 Numerical1.ps(数学中的数值计算)的第 4 页所示, Yaroslav Bulatov 的回答非常好.

编辑 3

Brett Champion 不仅指出 FullForm 是多余的,而且在生成 GraphicsComplex 的情况下,应用 Normal 将转换将复合体转化为基元.这可能非常有用.

例如:

lp = ListPlot[Transpose[{Range[10], Range[10]}],填充 ->底部];案例[案例[正常@lp,点[___],无穷大], {_?NumericQ, _?NumericQ}, 无穷大]

给出(正确)

<块引用>

{{1., 1.}, {2., 2.}, {3., 3.}, {4., 4.}, {5., 5.}, {6., 6.}, {7.,7.}, {8., 8.}, {9., 9.}, {10., 10.}}

感谢布雷特冠军.

最后,使用此答案中给出的一般方法的一种更简洁的方法,我发现 这里

OP 问题,就 ListPlot 而言,可以得到如下:

ListPlot@Cases[g, x_Line :>第一@x,无限]

编辑 4

更简单

ListPlot@Cases[plot, Line[{x__}] ->x,无穷大]

ListPlot@Cases[** Paste_Grphic _Here **, Line[{x__}] ->x,无穷大]

ListPlot@plot[[1, 1, 3, 2, 1]]

计算结果为 True

plot[[1, 1, 3, 2, 1]] == Cases[plot, Line[{x__}] ->x,无穷大]

When plotting a function using Plot, I would like to obtain the set of data points plotted by the Plot command.

For instance, how can I obtain the list of points {t,f} Plot uses in the following simple example?

f = Sin[t]
Plot[f, {t, 0, 10}]

I tried using a method of appending values to a list, shown on page 4 of Numerical1.ps (Numerical Computation in Mathematica) by Jerry B. Keiper, http://library.wolfram.com/infocenter/Conferences/4687/ as follows:

f = Sin[t]
flist={}
Plot[f, {t, 0, 10}, AppendTo[flist,{t,f[t]}]]

but generate error messages no matter what I try.

Any suggestions would be greatly appreciated.

解决方案

f = Sin[t];
plot = Plot[f, {t, 0, 10}]

One way to extract points is as follows:

points = Cases[
   Cases[InputForm[plot], Line[___], 
    Infinity], {_?NumericQ, _?NumericQ}, Infinity];

ListPlot to 'take a look'

ListPlot[points]

giving the following:

EDIT Brett Champion has pointed out that InputForm is superfluous.

ListPlot@Cases[
  Cases[plot, Line[___], Infinity], {_?NumericQ, _?NumericQ}, 
  Infinity]

will work.

It is also possible to paste in the plot graphic, and this is sometimes useful. If,say, I create a ListPlot of external data and then mislay the data file (so that I only have access to the generated graphic), I may regenerate the data by selecting the graphic cell bracket,copy and paste:

ListPlot@Transpose[{Range[10], 4 Range[10]}]

points = Cases[
  Cases[** Paste_Grphic _Here **, Point[___], 
   Infinity], {_?NumericQ, _?NumericQ}, Infinity] 

Edit 2.

I should also have cross-referenced and acknowledged this very nice answer by Yaroslav Bulatov.

Edit 3

Brett Champion has not only pointed out that FullForm is superfluous, but that in cases where a GraphicsComplex is generated, applying Normal will convert the complex into primitives. This can be very useful.

For example:

lp = ListPlot[Transpose[{Range[10], Range[10]}], 
  Filling -> Bottom]; Cases[
 Cases[Normal@lp, Point[___], 
  Infinity], {_?NumericQ, _?NumericQ}, Infinity] 

gives (correctly)

{{1., 1.}, {2., 2.}, {3., 3.}, {4., 4.}, {5., 5.}, {6., 6.}, {7., 7.}, {8., 8.}, {9., 9.}, {10., 10.}}

Thanks to Brett Champion.

Finally, a neater way of using the general approach given in this answer, which I found here

The OP problem, in terms of a ListPlot, may be obtained as follows:

ListPlot@Cases[g, x_Line :> First@x, Infinity]

Edit 4

Even simpler

ListPlot@Cases[plot, Line[{x__}] -> x, Infinity]

or

ListPlot@Cases[** Paste_Grphic _Here **, Line[{x__}] -> x, Infinity]

or

ListPlot@plot[[1, 1, 3, 2, 1]]

This evaluates to True

plot[[1, 1, 3, 2, 1]] == Cases[plot, Line[{x__}] -> x, Infinity]

这篇关于Mathematica:如何获取 plot 命令绘制的数据点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆