Mathematica:如何获取plot命令绘制的数据点?
当使用 Plot 绘制函数时,我想获取由 Plot 命令绘制的数据点集。
例如,如何获取以下简单示例中使用的点 {t,f} Plot 的列表?
f = Sin[t]
Plot[f, {t, 0, 10}]
我尝试使用一种将值附加到列表的方法,如 Jerry B. Keiper 的 Numerical1.ps(Mathematica 中的数值计算)的第 4 页所示,http://library.wolfram.com/infocenter/Conferences/4687/ 如下:
f = Sin[t]
flist={}
Plot[f, {t, 0, 10}, AppendTo[flist,{t,f[t]}]]
但无论我尝试什么,都会生成错误消息。
任何建议将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除了 Leonid 中提到的方法回答和我的后续评论,要实时跟踪慢速函数的绘制进度以查看发生的情况,您可以执行以下操作(使用 这个最近的问题):
等。
In addition to the methods mentioned in Leonid's answer and my follow-up comment, to track plotting progress of slow functions in real time to see what's happening you could do the following (using the example of this recent question):
etc.
这是获取所有数据点的非常有效的方法:
Here is a very efficient way to get all the data points:
根据 Sjoerd C. de Vries 的回答,我现在编写了以下代码,该代码可自动执行绘图预览(在 Mathematica 8 上测试):
除了显示绘图的进度之外,它还标记了当前绘图的 x 位置计算。
主要问题是,对于多个绘图,Mathematica 对最终绘图中的所有曲线应用相同的绘图样式(有趣的是,它不在临时绘图上)。
要将生成的数据存入变量
dest
,请使用选项SaveData:>dest
Based on the answer of Sjoerd C. de Vries, I've now written the following code which automates a plot preview (tested on Mathematica 8):
In addition to showing the progress of the plot, it also marks the x position where it currently calculates.
The main problem is that for multiple plots, Mathematica applies the same plot style for all curves in the final plot (interestingly, it doesn't on the temporary plots).
To get the data produced into the variable
dest
, use the optionSaveData:>dest
另一种方式,可能取决于实现:
Just another way, possibly implementation dependent:
只需查看绘图的结构(对于不同类型的绘图,结构会有一点不同)并使用类似的东西:
Just look into structure of plot (for different type of plots there would be a little bit different structure) and use something like that:
提取点的一种方法如下:
ListPlot“看一下”,
给出以下内容:
编辑
Brett Champion 指出
InputForm
是多余的。会起作用的。
也可以粘贴绘图图形,这有时很有用。比如说,如果我创建一个外部数据的 ListPlot,然后放错了数据文件(这样我只能访问生成的图形),我可以通过选择图形单元格括号、复制和粘贴来重新生成数据:
编辑2.
我还应该交叉引用并承认这一点雅罗斯拉夫·布拉托夫的回答非常好。
编辑 3
Brett Champion 不仅指出
FullForm
是多余的,而且在生成GraphicsComplex
的情况下,应用Normal
会将复数转换为基元。这非常有用。例如:(
正确地)给出
感谢 Brett Champion。
最后,使用这个答案中给出的一般方法的更简洁的方法,我发现这里
OP 问题,以 ListPlot 的形式,可以通过如下方式获得:
编辑 4
更简单的
or
or
其计算结果为
True< /代码>
One way to extract points is as follows:
ListPlot to 'take a look'
giving the following:
EDIT
Brett Champion has pointed out that
InputForm
is superfluous.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:
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 aGraphicsComplex
is generated, applyingNormal
will convert the complex into primitives. This can be very useful.For example:
gives (correctly)
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:
Edit 4
Even simpler
or
or
This evaluates to
True
例如,一种方法是将
EvaluationMonitor
选项与Reap
和Sow
结合使用One way is to use
EvaluationMonitor
option withReap
andSow
, for example