Adding Data Visualization to Python for Producing Graphs
06/02/2000
Related Articles |
Introduction
With the drudgery of last month's article behind us, let's explore the DISLIN data visualization package. If you do not have DISLIN installed, you can obtain the software from http://www.linmpi.mpg.de/dislin. DISLIN provides the Python programmer with a robust package capable of producing both 2D and 3D graphs. DISLIN also supports basic drawing commands (e.g. line, box, circle), which also allows it to function as a generic drawing tool. (This will be left for next month.) The combination of DISLIN and NumPy provide a formidable scientific computing package.
After installing the package, get a hold of the DISLIN Python Manual (available on the web site). This short document illustrates the basic drawing commands of the package and also provides programming examples in Python. There is also a full manual available that provides complete details about the myriad of commands available. Before you dig into the full manual, however, read on to discover a simpler way.
Quickplots
What originally caught my eye about DISLIN was the availability of Quickplots. These are basic DISLIN routines that allow you to generate complex plots with a single command. They are very nice as interactive tools when using Python from a command prompt.
Before we get started, you need to set up the environment. To allow easy access to DISLIN and NumPy functions, the following two commands should be executed at the beginning of each session (or script file), after which functions from the two libraries will be available in the common namespace.
>>> from dislin import *
>>> from Numeric import *
The simplest Quickplot routine is plot()
which draws a 2D graph containing data points connected by lines. To see this in action, open a Python command window and type along.
>>> x = arange(100.0)
>>> plot(x,sin(x/3)+cos(x/5))
>>> disfin()
The first command is a NumPy function creating an array with values of x=1.0,…,100.0. It is very important to note the .0 in 100.0 as that makes the numbers floating point. Not doing so would have generated an array of integers. Alternatively you could have used
>>>x = arange(100,Float)
which would have forced x
to be floating point. The plot()
command takes two arguments, each an array, and plots the second argument (as the dependent variable or vertical axis) against the first argument (as the independent variable or horizontal axis). In a more formal sense, the plot command plots f(x) against x. For this example I chose
a sum of two sinusoid functions.
The resultant plot is shown below. The disfin()
function tells the plotting package that you are done with the current graph and to render it. It also freezes the console until the right mouse button is pressed. After pressing the mouse button, the plot is cleared and the control is returned to the console.
In addition to the simple 2D plots, there are several Quickplot routines that enable 3D graphics. Each of these routines use the same basic arguments. The first argument is a matrix of values followed by two single-dimension arrays. In a three-dimensional plot there are three axes. The x and y axes form a plane (flat surface) from which the z axis rises. The values in the matrix are the heights (above or below) the x-y plane. The x and y single-dimension arrays provide scaling along those axes. Back again to formal notation, the 3D routines plot z=f(x,y) against x and y. For the 3D plots I chose
which is a two-dimensional sinusoid function.