Plotting Data

Intro

As a student in Science, you are going to have to make plots of data and functions fairly routinely. While spreadsheets can do some of this, they're really not meant for scientific data analysis (and generally make pretty poor-quality plots, too). Below are some detailed instructions for plotting data and functions using some common, freely available packages — SciLab, Gnuplot, and Octave (a matlab clone). Many, many other packages are available, but those above are free, widely used, and should work on most computers. Both of these come with considerable documentation and help, either in the package themselves or on their respective webpages. For the purposes of this exercise, we're going to assume that we want to first plot the function x2sin(x), and then time-vs-luminosity data supplied for Assignment #1.

Scilab

Scilab is a fairly general-purpose numerical computation package for scientific data crunching. It does a lot more than plotting data and has similar functionality to expensive commercial packages like Matlab or IDL. First, start up Scilab and there will be a command window. Scilab doesn't really deal with analytical functions; so to plot a function, we will evaluate the function at a series of points and plot that. The commands, with comments, will set up our first plot:
-->// We'll make this plot from -3 pi to + 3 pi 
-->// in intervals of 0.05
-->x = -3.*%pi:0.05:3.*%pi;
-->y = sin(x).*x^2; 
-->clf; plot2d(x,y);
The above lines create a line of data x with elements going from -3π to +3π in increments of 0.05; then x2sin(x) is calculated for each of those points, and finally we clear the plot window (clf) and make the plot. Note if we wanted to plot x3sin(x) on the same plot we could add it like so.
-->y2 = sin(x).*x^3;
-->plot2d(x,y2,style=3);
If we are plotting tabulated data, than we proceed in mostly the same way:
-->//enter in sample data
-->masses = [0.1 0.2 0.3 0.4 0.5];
-->times = [1600 580 420 330 290];   

-->//Plot data, first as line plot, then with symbols;
-->clf; plot2d(masses,times); xtitle("","Mass","Time (Myr)");
-->clf; plot2d(masses,times,style=-1); 
-->xtitle("","Mass","Time (Myr)"); 

-->// Set Plot range to be a bit more what we want
-->clf; plot2d(masses,times,style=-1,rect=[0,0,.6,2000]); 
-->xtitle("","Mass","Time (Myr)"); // Plot title, x-axis label, y-axis label
To plot the data for Assignment 1, we don't have to type in the data by hand, as the helpful instructor has given you a file with the data in it; the variables are named time and luminosity, and the units are in minutes and 1047 erg/s.
-->load('xray-data.scilab');
-->plot2d(time,luminosity);          // with lines
-->plot2d(time,luminosity,style=-1); // overplot with symbols
-->xtitle("","Time (min)","Luminosity (10^47 erg/s)");

Once this is done, you can use File->Export in the plot window to save the plot to a file or print it out. You can find the mean of the luminosity data very simply (note no semicolon at the end):

--> mean(luminosity)

Gnuplot

Gnuplot is a data- and function-plotting package that works in most environments. It is terrific for making quick plots of data you already have in text files. Unlike Scilab, gnuplot works on data files containing your data. So first, enter your data into a text file like `data.dat'; using the same sample data as above, a data file containing the following
#our test data
#mass time
 0.1  1600
 0.2  580 
 0.3  420 
 0.4  330 
 0.5  290
would work. Then start up gnuplot; you'll get a gnuplot> prompt.
gnuplot> plot 'data.dat' using 1:2

gnuplot> # set the range to something we want
gnuplot> set xrange [0.01:0.6]
gnuplot> replot

gnuplot> # make fancier
gnuplot> plot 'data.dat' using 1:2 title "My Data", 100/x title "Fit"

gnuplot> set xlabel "Mass"
gnuplot> set ylabel "Time (Myr)"
gnuplot> replot

gnuplot> # depending on your platform, you may be able to
gnuplot> # print or save from the plot window.  Otherwise,
gnuplot> # something like this should give you a graphics
gnuplot> # file with the output

gnuplot> set term pdf
gnuplot> set output 'data-and-fit.pdf'

gnuplot> replot
gnuplot> quit

gnuplot> # or 

gnuplot> set term png
gnuplot> set output 'data-and-fit.png'
gnuplot> replot
gnuplot> quit
Note how we plotted the function; we just told gnuplot to plot 100./x; it already knows the x values because it automatically chooses a range to plot in. So if we want to plot x2sin(x), we can just tell it
gnuplot> plot sin(x)*x*x
gnuplot> # set the range to be what we want
gnuplot> set xrange [-9.42:9.42]
gnuplot> set xlabel 'x'
gnuplot> set ylabel 'x^2 sin(x)'
gnuplot> replot
To plot the data for assignment 1, we download the file 'xray-data.dat' and plot it:
gnuplot> plot 'xray-data.dat' using 1:2 with linespoints title 'Xray data'
gnuplot> set xlabel 'Time (min)'
gnuplot> set ylabel 'Luminosity (10^{47} ergs/s)'
gnuplot> replot
Gnuplot has pretty powerful curve fitting capabilities. The easiest way to find the mean to the data may be to fit a straight line to the data:
gnuplot> f(x) = a
gnuplot> fit f(x) 'xray-data.dat' via a
...

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a               = X.XXXXX          +/- 0.004358     (0.3015%)
gnuplot> plot f(x) title 'Mean', 'xray-data.dat' using 1:2 with linespoints title 'Xray data'
The first line defines a function f(x), which is simply a constant, a. The second line fits f(x) to the data in xray-data.dat, by adjusting the parameter a; eventually gnuplot tells you the vest fit value, along with an error estimate of the fit parameter. The last line just plots the fitted function along with the data.

Octave/Matlab

Octave, a partial clone of MatLab, is, like SciLab, a fairly general-purpose numerical computation package for scientific data crunching - but it is a little less full-featured than SciLab. Unless you already have a lot of experience with Octave or Matlab, I'd suggest using SciLab. First, start up octave and there will be a command window. To plot a function, we will evaluate the function at a series of points and plot that. The commands, with comments, will set up our first plot:
octave-3.2.3:1> % We'll make this plot from -3 pi to + 3 pi
octave-3.2.3:1> % in intervals of 0.05
octave-3.2.3:1> x = -3.*pi:0.05:3.*pi;
octave-3.2.3:2> y = sin(x).*x.*x;
octave-3.2.3:3> clf;
octave-3.2.3:4> plot(x,y);
The above lines create a line of data x with elements going from -3π to +3π in increments of 0.05; then x2sin(x) is calculated for each of those points, and finally we clear the plot window (clf) and make the plot. Note if we wanted to plot x3sin(x) on the same plot we could add it like so.
octave-3.2.3:5> y2 = sin(x).*x.*x.*x;
octave-3.2.3:6> plot(x,y,x,y2);
If we are plotting tabulated data, than we proceed in mostly the same way:
octave-3.2.3:9> masses = [0.1 0.2 0.3 0.4 0.5];
octave-3.2.3:10> times = [1600 580 420 330 290];
octave-3.2.3:11> clf();
octave-3.2.3:12> plot(masses,times,'-o');
octave-3.2.3:13> xlabel("Masses") ;
octave-3.2.3:14> ylabel("Time");
octave-3.2.3:15> replot;
To plot the data for Assignment 1, we don't have to type in the data by hand, as the helpful instructor has given you a file with the data in it; the variables are named time and luminosity, and the units are in minutes and 1047 erg/s.
octave-3.2.3:23> load 'xray-data.mat'
octave-3.2.3:24> plot(time,luminosity,'-o')
octave-3.2.3:25> xlabel("Time(min)");
octave-3.2.3:26> ylabel("Luminosity (10^{47} erg/s)");

Once this is done, you can use File->Export in the plot window to save the plot to a file or print it out. You can find the mean of the luminosity data very simply (note no semicolon at the end):

octave-3.2.3:27> mean(luminosity)