8

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?

alabid
  • 261
  • 1
  • 7
  • 3
    You might be interested in having a look at [matplotlib](http://matplotlib.sourceforge.net/), a 2D plotting library in python. It's under a BSD-style license, so you can read and use source code to your heart's content. – Cascabel Jan 23 '11 at 07:29
  • I think @Jefromi's comment is the best possible answer, as nobody will be able to tell you how Matlab or Mathematica really work. There are many, many subtle issues to solve in a robust graphics library, no easy answer for you, sorry – Dr. belisarius Jan 23 '11 at 07:46
  • The [underlying rendering of plots in sage](http://hg.sagemath.org/sage-main/file/f24ce048fa66/sage/plot/plot.py#l1) is also done with matplotlib. – Simon Jan 23 '11 at 09:00
  • 1
    While belisarius is right (the subtleties will take an entire career to understand) you can get some good hints as to the basics from other questions on here. Yaroslav's fantastic answer to this one: http://stackoverflow.com/questions/4572183/strange-sinx-graph-in-mathematica gives a real insight into the basics of the process. – Tim Jan 27 '11 at 17:44

4 Answers4

6

Well, with some encouragement from belisarius, here's a my comment as an answer: try looking at matplotlib. From the home page:

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits.

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.

Gnuplot is a portable command-line driven graphing utility for linux, OS/2, MS Windows, OSX, VMS, and many other platforms. The source code is copyrighted but freely distributed (i.e., you don't have to pay for it). It was originally created to allow scientists and students to visualize mathematical functions and data interactively, but has grown to support many non-interactive uses such as web scripting. It is also used as a plotting engine by third-party applications like Octave. Gnuplot has been supported and under active development since 1986.

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.

MaxRecursion Automatic the maximum number of recursive subdivisions allowed Method Automatic the method to use for refining curves PerformanceGoal $PerformanceGoal aspects of performance to try to optimize PlotPoints Automatic initial number of sample points

From the MaxRecursion and PlotPoints, 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 some Method for this, and a PerformanceGoal to guide it...

Cascabel
  • 479,068
  • 72
  • 370
  • 318
1

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.

Mikhail Poda
  • 5,742
  • 3
  • 39
  • 52
  • I suspect MATLAB has a number of rendering engines besides OpenGL, though I can't state that for a fact.. It is common for such cross-platform environments to use the native 2D/3D graphics API of the system they are running on (GDI/DirectX, X11, Quartz, ...) – Amro Jan 23 '11 at 13:39
0

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.

Why code it yourself when someone has already solved the problem for you?

Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • 1
    So computer science classes never deal with, say, creating compilers, because there are already compilers, right? – Cascabel Jan 23 '11 at 17:44
  • You're an idiot! Read what I wrote and read it in context before you respond to me in future. (or just dont!) Im sure I could nitpick at your answer and come up with a similar smartass statement if I wanted to act like an ignoramus. – Maxim Gershkovich Jan 25 '11 at 05:57
  • 1
    Steady on. The OP doesn't want to draw a graph, s/he wants to learn some of the details of how graphs are drawn. Jefromi's suggestion of looking at the details of matplotlib helps with that; yours does not. – Tim Jan 27 '11 at 17:39
0

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

FullForm[Plot[Sin[x], {x, 0, 2 Pi}]]

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.

Samsdram
  • 1,615
  • 15
  • 18