Intro to Scientific Computing
PHYS 27/193
Physics Department
University of the Pacific

Scientific Visualization

When doing science, you will often need to "look" at some data which you have taken during an experiment or produced in a simulation, usually to compare such data with a theoretical prediction from a model in mathematical form. For this you will need software to do scientific visualization. You are certainly already familiar with the simplest form of data visualization, i.e. plotting or graphing x and y values on a Cartesian grid.

Let's examine this idea of scientific visualization. Here is a definition taken from a tutorial on the website of the Georgia Tech Scientific Visualization Laboratory--an entire institute set up to help scientists and engineers with visualization at GT.

Scientific visualization, sometimes referred to in shorthand as SciVis, is the representation of data graphically as a means of gaining understanding and insight into the data. It is sometimes referred to as visual data analysis. This allows the researcher to gain insight into the system that is studied in ways previously impossible.

It is important to differentiate between scientific visualization and presentation graphics. Presentation graphics is primarily concerned with the communication of information and results in ways that are easily understood. In scientific visualization, we seek to understand the data. However, often the two methods are intertwined.

Thus scientific visualization is a means of using graphical techniques to allow the incredibly powerful, massively parallel processing, high performance, neuro-optical wetware (your eyes and your brain), to look for patterns in the data.

I would also argue that once you have understood your data, the very next step is to present your results to others! To do that, you take the graphics of your visualization studies, clean them up, add helpful guides and text, and make presentation graphics for publication or oral presentations. So, the two are very closely related.

Shortly, we will get a tool which will serve us for both aspects.


Examples

First however, follow the link below to look at a few very impressive visualizations of some interesting data from the Wall Street Journal's Classroom page.

Look at these graphs

prepared by Karl Hartig, for the from the Wall Street Journal Classroom Edition.

These are best viewed in their PDF versions (because you can zoom in deeply), reached by clicking on the PDF icon below the image on each page.

In particular, be sure to look at

SciVis Apps

There are many software applications for doing SciVis.

A non-authoritative list is here.
Have a look.

Many of these are very powerful, and many are very expensive (>$3000).

Your computer may already have a software program to graph data (for example, Excel and other spreadsheets used in business computing will make simple graphs of data). Also, you may have had experience with other scientific analysis programs, such as Matlab, Maple, and others. These are powerful and very good applications. If you have experience with these, will come in useful since the more applications you are familiar with, the more versitile you will be as a scientist or engineer. Furthermore, your career is likely to involve several employers as you begin to build expertise in you area. Different employers will have different resources; Lawrence Livermore National Laboratory, for example, will have many different software packages, and may not blink if you ask your boss to buy Matlab at $10,000 because that is what you are familiar with.

My philosophy in this course is to, as often as possible, provide you with free tools, which you can "take with you" to your next position, and the one after that. Cygwin provides a platform for many of these tools, and Gnuplot is one of the easiest to use.

Gnuplot

"I can't believe this is free!"
      Ben Rosenkrantz,  Physics Major 2006

That pretty much sums it up. Gnuplot is an amazingly powerful plotting and data fitting program capable of making 2 and 3-D plots such as y = f(x), and z = f(x,y). I use it for most of the data visualization in my research.

Getting Started with Gnuplot

First, let's keep our gnuplot exercises in a separate directory.

Open an xterm, and make a subdirectory below your home dir called: gp (you can give it a different name if you like).
Now cd to gp.

We'll save gnuplot files and data here.


We'll use Gnuplot to display three primary types of plots, often on the same graph for comparison. These are:

We will also do

Now at the xterm prompt, type gnuplot.

This will start the gnuplot program and you should see something like this on your screen:


        G N U P L O T
        Version 4.2 patchlevel 2 
        last modified 31 Aug 2007
        System: CYGWIN_NT-5.1 1.5.25(0.156/4/2)

        Copyright (C) 1986 - 1993, 1998, 2004, 2007
        Thomas Williams, Colin Kelley and many others

        Type `help` to access the on-line reference manual.
        The gnuplot FAQ is available from http://www.gnuplot.info/faq/

        Send bug reports and suggestions to 


Terminal type set to 'x11'
gnuplot> 

This last line: gnuplot> is the Gnuplot prompt which is where you type gnuplot commands.


Builtin Math Functions

In the examples below, be sure to type things exactly as they appear.
Any quotes (") and commas (,), etc. are important!

At the gnuplot> prompt, type

gnuplot> plot x
and hit Enter. For now I will highlight what you are to type in red.

You should see a graph window with a diagonal red line. This is a plot of the function f(x) = x.
Gnuplot's default plotting range for x is [-10:10] (the range between x = -10 and x = +10).

You just plotted your first function.


Next, let's add a quadratic function: f(x) = 2x2 -3x + 1

To do this, hit the UP arrow to bring back the previously entered command and now add the following, then press Enter.

gnuplot> plot x, 2*x**2 - 3*x + 1
Does your plot look correct? (There should be a line and a parabola).

Notice how we entered 2x2 -3x + 1, as
2*x**2 - 3*x + 1.

We must explicitly tell gnuplot about multiplication with the * operator.

For the quadratic term, x2, we use the notation x**2, where the ** operator represents exponentiation. For example, we could do x**0.5 for the square root of x, however gnuplot has a builtin function: sqrt(x) for this.

Also, we have two functions plotted, f(x) = x, a linear function, and f(x) = 2x2 -3x + 1, a quadratic function.
They are both plotted with one plot command, separating the two functions x and 2*x**2 - 3*x + 1 with a comma ,
We could plot several more functions on this same graph, we would simply put commas between each function to be plotted.


Step 2

Let's make the plot a bit easier to read. At the prompt, type:
gnuplot> set zeroaxis
Then use the UP arrow to recall your last plot command. Press Enter.
Now you should have dotted lines along the x = 0 and y = 0 axes. It's a little easier to see where your functions are located relative to the origin with these.

Now, let's zoom in a bit. The interesting area of the graph is where the two functions intersect. Let's narrow the plot range.

Using the UP arrow, recall your previous plot command.

Now use the LEFT arrow to move the cursor to just after the plot command, and insert a range specifier [-0.5:2] as shown below:

gnuplot> plot [-0.5:2] x, 2*x**2 - 3*x + 1
Notice what this does (press Enter to execute the plot command if you haven't already). The graph should zoom in on the area btween x = -0.5 and x = 2.0.

You can also limit the y range by adding a second range specifier for the y axis.

gnuplot> plot [-0.5:2][-0.5:3] x, 2*x**2 - 3*x + 1
which also limits the y range of the plot to lie between particular values.
The format for the range is [Xmin:Xmax][Ymin:Ymax].
One could allow the x range to be free while specifying the y range with [:][Ymin:Ymax],
and similarly [Xmin:Xmax][:] to leave the y range free. This however is equivalent to simply [Xmin:Xmax]


Step 3

Still the graph needs more. Let's notice some things.

There is a key telling us which plotted lines correspond to which function.
The default Key is to draw short examples of the lines used and copy the function that you typed to plot beside the associated line.

Notice that the key is run over by the plot lines. You can move the key to another place by typing

gnuplot> set key bottom right box
gnuplot> replot
The replot command re-draws the last plot command.
Notice where the Key is now, in a more conveniently viewable location, and it has a nice box around it.

You can get rid of the box if you don't like it by typing

gnuplot> set key nobox
gnuplot> replot

A graph also needs labels. We can add these with

gnuplot> set xlabel "x"
gnuplot> set ylabel "f(x)"
gnuplot> set title "My First Plot"
gnuplot> replot
This should label the x and y axes, and put a title above the the graph. Verify that it does so.

Also, notice that the words you want to appear in the labels must be enclosed in ""s. Words such as these in labels, etc. are called strings, and all "strings" must be enclosed in ""s

You can even clean up the Key a bit, by giving labels to the lines.
Look at the Key as it is now in your plot.
Call back your previous plot command with the UP arrow, and add the following title option to each function

gnuplot> plot [-0.5:2][-0.5:3] x title "linear", 2*x**2 - 3*x + 1 title "quadratic"
Do you see what that did to the Key?

You can learn more about the possible settings for the Key by typing

gnuplot> help set key
This will display Help contents on the options and usage of the set key command.
Keep hitting the spacebar until you get the gnuplot> prompt back.



One more thing. Suppose we want a bit finer resolution on the x axis than small tics in steps of 0.5.
Suppose we wanted them every 0.25 instead. We can accomplish this by doing

gnuplot> set xtics 0.25
How do you think you would adjust the y tics?


At this point, your plot should look like this.



Summary

Let's summarize what we've done so far. If you need to, go back though the above exercise and make sure you know how to do each of these things.




Next: More Fun with Gnuplot