Simple plots

By Edward Sternin, Brock University

This is an introductory notebook to various ways of plotting and analyzing experimental data, as appropriate to second-year Physics majors. Feel free to choose your own preferred package, but mastering one of the tools in this selection is strongly recommended, as the demands on the software will become more significant in later years of the program, and so other packages (notably, excel) will not have sufficient capabilities. Choose a package that has "long legs".

A large number of software packages exist, and personal preferences play a great role in selecting which one will end up as your favourite go-to tool. Here we will restrict ourselves to a comparison of a few packages. The criteria for selecting them are simple:

Our selection: extrema (or its predecessor, physica), gnuplot, and octave (a free alternative to Matlab). All of these are inherently interactive programs, and so they are normally used through a command-line interface (CLI) or their own native Graphics User Interface (GUI). In this notebook, we will emulate that behaviour by using scripts, which is actually an advanced use mode for most programs. To get a true feel for the programs, please attempt to use their native interfaces, guided by the sample command selections below.

From a wide field of significant competitors not included here are Maple (a commercial package) and grace (a.k.a xmgrace), and Mac-world's Igor. These are reasonable alternatives so if you have significant skills in their scripting, they will be appropriate to continue to use, though they do not satisfy all of the selection criteria.

To begin with, configure jupyter's work directory and import some libraries that enable us to display graphics files in this notebook.

Getting the data into the program

The two basic ways are: direct entry, where you embed the data in the script, and external file read. The former has the advantage of portability (one file to keep track of for any project), the latter has the advantage of compactness (a small script acting on a series of potentially large data files).

All three programs allow direct entry of data, with minor syntactic differences:

physica, direct entry:
x=[1:8]
y=[0.05;0.10;0.14;0.19;0.25;0.30;0.34;0.40]
dy=[0.02;0.07;0.01;0.04;0.05;0.10;0.02;0.04]
set xlabel `Time, s'
set ylabel `Distance, m'
set pchar -12
graph x,y,dy
gnuplot, direct entry:
\$DATA << EOD
1 0.05 0.02
2 0.10 0.07
3 0.14 0.01
4 0.19 0.04
5 0.25 0.05
6 0.30 0.10
7 0.34 0.02
8 0.40 0.04
EOD

set xlabel 'Time, s'
set ylabel 'Distance, m'
plot \$DATA with errorbars pt 6
octave/matlab, direct entry:
x=linspace(1,8,8);
y=[0.05 0.10 0.14 0.19 0.25 0.30 0.34 0.40];
dy=[0.02 0.07 0.01 0.04 0.05 0.10 0.02 0.04];
plot(x,y,'go');
xlabel("Time, s");
ylabel("Distance, m");

To demonstrate the plots within this notebook, we need to communicate to the external program, like extrema, or load on-the-fly a kernel magic that will enable execution of an external program directly embedded in this python-based notebook.

We will default all of the settings that control the appearance of the graphs, for simplicity. All of these programs can produce publication-quality plots through a series of adjustments to their appearance. This will be addressed in the following sections.

eXtrema

An experimental eXtrema kernel exists, but it is not yet installed system-wide, so each user must install it into their own file space. A separate jupyter notebook shows how to do it. That notebook needs to be run only once, and then this notebook's kernel needs to be restarted, to read in the newly added eXtrema kernel. If you have already done that at some point, the eXtrema kernel should included in the drop-down list of kernels under the Kernel menu above.

Once extrema kernel is available in the drop-down menu of kernels, you do not need to repeat the above and the line magic %eXtrema and cell magic %%eXtrema should be available. Note that this kernel uses a simplified batch-mode version of extrema which may not have some commands implemented yet. The kernel returns an image file which we can then show within this notebook.

gnuplot

Kernel magic for gnuplot exists:

octave

Kernel magic for octave exists:

External file processing

With only minor changes the same scripts can be applied to external data files. Scripts below all accomplish the same basic read from a data file, a plot, followed by a number of adjustments of settings that approach the publication-quality standards of appearance. The output is not shown, but saved into into a vector-graphics file, ready to be uploaded/included into overleaf. Encapsulated PostScript (.eps) or SVG (.svg) vector-graphics formats work best, as they scale without introducing "jaggies" and always display at the resolution of the device (typically, at about 100dpi on a monitor screen, and at 600dpi on paper).

As of 2019, overleaf has support for SVG inclusion (\usepackage{svg} and \includesvg{image.svg}).

First, let's create the file of data to be plotted by each of the programs in turn:

eXtrema

If you installed the experimental jupyter eXtrema kernel that introduces cell-level magic %%eXtrema, you can run eXtrema commands directly within this notebook. However, some functionality is still missing, so a better way at the moment is to generate a macro file and then execute it from within eXtrema invoked outside of jupyter. This uses a full interactive version of eXtrema, so at the end when it processes your basic.pcm script that ends with a quit choose Yes when it offers to terminate the run. The resulting .eps file can be seen with a PDF viewer (e.g. evince) and is ready to be uploaded to overleaf.

gnuplot

Data importation is actually difficult to achieve in gnuplot because it's primarily a plotting program, and data manipulation is not really its strength. On the other hand, plotting data from external ASCII data files is particularly easy, as this is precisely what gnuplot was originally designed for.

Using the same data file as above:

octave

The use of comments makes the following script self-explanatory, hopefully. octave kernel actually will return a PNG graphic that will show up when you execute the script, in addition to writing out the EPS file.

Use an external PDF viewer to examine and compare the three EPS files that got created, physica.eps, gnuplot.eps and octave.eps.

Why not python?

This is an obvious question to ask, since we are already using a python-based jupyter notebook to run this discussion and comparison. python is a powerful programming environment, with a large number of packages available, of varying quality. python is fully capable of generating simple graphs. However, in my experience, extending this to publication-quality outputs is more difficult in python than in almost any other environment.

More importantly, its object-oriented style of coding is difficult to master on a casual basis, and is easily forgotten, if used rarely. As an illustration, I find that this

   data=numpy.loadtxt('exp1.dat')
   x = data[:,0]
   y = data[:,1]
   dy= data[:,2]
   def f(x, a, b): return a*x**2 + b
   scipy.optimize.curve_fit(f, x, y, p0=[1, 0])
a lot less readable than this (for gnuplot)

   f(x) = a*x**2+b
   a=1
   b=1
   fit f(x) 'exp1.dat' via a,b
or this (for physica)

   read exp1.dat x,y,dy
   scalar\vary a,b
   fit y=a*x^2+b

In other courses of the Physics program, python may prove more suitable, especially if specific-purpose libraries are available to help, but for the everyday scientific tasks of analyzing and plotting data, procedurally-based languages offer an easier way to perform them.

For an interesting, if somewhat discouraging read, look at https://realpython.com/python-matplotlib-guide/ just to see how idiosyncratic python's interface can be, from stucture to terminology. Without comment, below is a brief example of a properly-syntaxed "simple" plot in python.

The output files are shown below as bitmap images, at the resolution of the web interface of jupyter, just for illustration. Their bitmap renderings may not do them justice. The images themselves are vector graphics and will scale properly, without "jaggies", when included in a LaTeX/overleaf document.