像mathematica这样的程序如何绘制图表以及如何制作这样的程序?
我一直想知道mathematica 和mathlab 等程序如何如此优雅且快速地绘制函数图。谁能向我解释他们是如何做到这一点的,此外,我该如何做到这一点?它与计算机编程或数学的某个方面或课程有关吗?那么哪个?
I've been wondering how programs like mathematica and mathlab, and so on, plot graphs of functions so gracefully and fast. Can anyone explain to me how they do this and, furthermore, how I can do this? Is it related to an aspect or course in Computer Programming or Math? Which then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,在贝利萨留的一些鼓励下,这是我的评论作为答案:尝试查看 matplotlib。从主页:
它最初受到 MATLAB 绘图功能的启发,尽管从那时起它已经发展了很多。它是可靠的软件 - 而且它是开源的,在 BSD 许可证下,因此您不仅可以阅读源代码,还可以破解它并以任何您喜欢的方式使用它。
您可以查看的另一个地方是 gnuplot。它不是常见的开源许可证之一,但它肯定是开源的,具有一些修改权限等。
它也可以进行 3D 绘图,这是 matplotlib 不具备的功能,而且它已经存在了很长时间。我首先想到 matplotlib 的原因是它旨在作为高级语言的库,而不是独立的应用程序,所以我猜它可能更容易阅读。
另一项建议是,为了了解 Mathematica 在幕后所做的事情,请查看 Plot 文档。特别是,如果您查看可用的选项,您可以推断出一些事情。
从
MaxRecursion
和PlotPoints
,你可以看到它正在做初始采样然后以某种方式决定哪些区域需要细分(重新采样)以获得准确的绘图视图。从那时起,它就变得神奇了:有一些Method
可以实现这一点,还有一个PerformanceGoal
来指导它......Well, with some encouragement from belisarius, here's a my comment as an answer: try looking at matplotlib. From the home page:
It was originally inspired by MATLAB's plotting capabilities, though it's grown a lot since then. It's solid software - and it's open source, under a BSD license, so not only can you read the source, you can hack on it and use it in whatever you like.
Another place you could look is gnuplot. It's not one of the common open source licenses, but it's certainly open source, with some permissions to modify and such.
It does 3D plotting as well, which matplotlib doesn't do, and it's been around a lot longer. The reason I thought of matplotlib first is that it's intended as a library for a higher-level language, not a stand-alone application, so I'm guessing it might a bit easier for you to read.
One other suggestion, just to get an idea of the sorts of things Mathematica is doing under the hood, is to look at the documentation for Plot. In particular, if you look at the available options, you can deduce things.
From the
MaxRecursion
andPlotPoints
, you can see that it's doing an initial sampling then somehow deciding which regions need to be subdivided (resampled) to get an accurate view of the plot. And from there on, it's magic: there is someMethod
for this, and aPerformanceGoal
to guide it...对于 MATLAB,由于其跨平台要求,没有使用 OpenGL 的替代方案。 MATLAB 运行时是用 C++ 编写的,非轴 GUI 使用 Java Swing。因此 MATLAB Plot 可能是 C++/OpenGL/Swing 的混合体。
实际上,MATLAB 图形比视频游戏图形复杂得多。我认为更容易找到有关视频游戏图形的教程,然后将其“缩小”为 MATLAB 功能,例如用相同颜色绘制一条线。
最重要的概念可能是变换矩阵。
For MATLAB, because of its cross-platform requirement there is no alternatives as using OpenGL. MATLAB runtime is written in C++ and non-axis GUI uses Java Swing. Therefore MATLAB Plot is probably a C++/OpenGL/Swing mixture.
In reality MATLAB graphics is much less complex then a video game graphics. I think it is easier to find tutorials on video game graphics and then "downsize" it to MATLAB functionality, like drawing a single line with the same color.
The most important concept is probably Transformation Matrix.
基本上,大多数绘制任何类型的图形(特别是任何具有合理复杂性的图形)的程序都会使用某种类型的第三方库。
使用的特定库取决于正在使用的编程语言。
例如:
对于 .Net 应用程序,您可以使用 Crystal 报表。 http://en.wikipedia.org/wiki/Crystal_Reports
对于 Java,您可以使用 JFreeChart。 http://www.jfree.org/jfreechart/
等等...
您可能会找到许多适用于您决定使用哪种语言进行编码的库。
如果您想在特定项目中完成此功能,我建议使用库,特别是如果您是初学者。由于许多问题,例如跨平台兼容性、图形渲染优化(即:确保图形渲染快速且“漂亮”)、与元素定位相关的数学等许多问题,这些图形库的实现方式的内部复杂性将非常显着。图表等。
最后,我怀疑您是否会找到该主题的特定课程(或需要它们),因为再次排除非常特定的情况,程序员将始终使用已经存在的库。
Basically most programs that plot any type of graph (particularly any graphs of reasonable complexity) will use some type of third party libraries.
The specific library used would depend on the programming language that is being used.
For example:
For a .Net application you might use Crystal reports. http://en.wikipedia.org/wiki/Crystal_Reports
For Java you might use JFreeChart. http://www.jfree.org/jfreechart/
And so on...
You will likely find numerious libraries for whatever language you decide to code in.
If you want to accomplish this functionality in your specific project I suggest using a library especially if you are a beginner. The internal complexities of how these graph libraries are implemented would be significant because of many issues such as cross platform compatibility, graphic rendering optimizations (ie: making sure the graphics render quickly and ‘prettily’), the maths associated with the positioning of elements on the graph and so forth.
Lastly I doubt you will find specific courses in this subject (or require them) as again excluding VERY specific cases programmers will always use libraries that already exist.
一个好的起点是了解图形有语法,并且您在收到绘图命令后想要构建的是图形的符号表示。对于 Mathematica,您可以执行类似
查看 Mathematica 使用的内部表示形式的操作。基本上,您需要根据颜色和坐标来描述要绘制的线段 (2D) 或网格 (3D)。此外,还需要有关图形比例以及如何绘制刻度线、标签轴等的信息。
这将我们引向问题的核心,如何确定要从函数和函数中绘制的线段范围?如果你仔细研究绘图的帮助文件,你会看到一些东西。首先有一个绘图点选项和一个 MaxRecursion 选项。这让我相信(这只是一个有根据的猜测,但这就是我的做法)Mathematica 在范围内的偶数间隔上绘制初始点数以获得起始值。下一部分是识别变化超过某个阈值的区域,然后对更多点进行采样,直到线段中任意两点之间的“变化”低于阈值。 Mathematica 递归地执行此操作,因此有 MaxRecursion 选项。
到目前为止,我对变化率的定义还相当模糊。描述变化的更有用的方法是在线段上取 3 分。假设第 1 点和第 3 点之间存在线性关系,并假设这种线性关系,对第二点进行预测。如果此预测的误差足够低,则考虑下一组三个点。如果误差高于阈值,那么您应该在此区域中采样更多点,直到满足阈值。通过这种方式,您将需要相对较少的曲线相对笔直的点,而更多的曲线在“有趣”的部分,其中曲线向新的方向弯曲。您绘制的曲线的平滑度将与您在点的线性预测中愿意容忍的误差成正比。
A good place to start is to understand that there is a grammar to graphics and what you want to construct upon receiving a plot command is a symbolic representation of the graph. For Mathematica, you can do something like
to see the internal representation Mathematica uses. Basically you need to describe the line segments (2D) or meshes (3D) you want to draw in terms of their color and coordinates. Also, there needs to information about the scale of the graph and how to draw tick marks, label axes, etc.
This leads us to the heart of the question, how do you determine the line segment you want to draw from a function and a range? If you dig around in the help file for plot, you see a few things. First there is a plot points option and a MaxRecursion option. This leads me to believe (and this is just an educated guess, but it is how I would do it) that Mathematica plots the initial number of points on an even interval over the range to get a starting value. The next part is to identify regions where change exceeds some threshold and then to sample more points until the "change" between any two points in your line segment is below a threshold. Mathematica does this recursively, hence the MaxRecursion option.
So far I have been pretty vague about defining rate of change. A more useful way to describe change is to take 3 pts on your line segment. Assume a linear relationship between the 1st and 3rd point and, assuming this linear relationship, make a prediction about what the 2nd point would be. If the error of this prediction is sufficiently low, then consider the next group of three points. If the error is above a threshold, then you should sample some more points in this region until the threshold is met. In this way you will require relatively few points where the curve is relatively straight and more at the "interesting" parts where it bends in new directions. The smoothness of the curve you draw will be proportional to the error you are willing to tolerate in the linear prediction of points.