Plotting GOLEM discharge data with Python

Table of Contents

1 Introduction

Python is a popular, mlutiplatform, object oriented and interpreted language. Today there are numerous libraries that extend this language and make it possible to accomplish various tasks with Python. One of these libraries is matplotlib, a library designed mainly for plotting and figure generation purposes. One of it's design goals is to provide an interface and feel similar to the one found in Matlab, however, it does not intend to simply clone it's functionality.

Matplotib internally uses n-dimensional arrays for data representation. This array interface is provided by the NumPy library, previously known as the Numerical Python library.

For obtaining data from a web server the Python standard library collection provides the urllib2 library.

2 Required software

  • python interpreter, version 2.7 recommended
  • matplotlib library
    • numpy library, fundamental library provided by the SciPy (scientific computing in python) project

2.1 Installation

2.1.1 From source code

You can use the easy_install script that comes with the python interpreter or the PIP installer to install the libraries from the python library archive.

2.1.2 Linux or *BSD

Most modern Linux distributions provide packages containing these libraries and on *BSD they can be found in the ports collection.

2.1.3 Windows

Installation instructions can be found on the project homepages referenced above. The easiest way is to install the Enthought Python distribution with the academic license.

3 Example script

This script is split into four phases:

  1. Import (or from a C programmer's point of view "include") the library functions
  2. Obtain the raw data from the GOLEM web server with urllib2
  3. Transform the data into a form understood by matplotlib with NumPy
  4. Plot the data and adjust the figure appearance with matplotlib

Save the script as a text file with a *.py appendix in a directory of your choice. On Linux, that could be ~/golem-test/python_script.py

#!/usr/bin/python2   
#a line starting with the character '#' is a comment and is disregarded by the interpreter
#the first line is used on *NIX systems to determine the program that should process the script

#PHASE 1
import matplotlib.pyplot as plt     # import the pyplot subsystem of the matplotlib library and bind it with the name 'plt'
from numpy import loadtxt     #from the numpy library import only the loadtxt function.
                #This automatically imports any other functions that loadtxt needs
from urllib2 import urlopen     #import the urlopen function from the urllib2 library

#PHASE 2
server = "http://golem.fjfi.cvut.cz/"     #the address of the server
shot = 6213     #discharge number
data_directory = "basicdiagn/" #directory within the discharge directory
data_file = "Uloop_dp.csv"     #the path to the file containing the data within the data_directory
downloaded_file = urlopen(server + "shots/" + str(shot) + "/" + data_directory + "/" + data_file)
                #this function returns a file-like object by downloading the file http://golem.fjfi.cvut.cz/shots/6213/basicdiagn/Uloop_dp.csv

#PHASE 3
data=loadtxt(downloaded_file, delimiter=';')     #transforms the file contents to a 2D array, columns are separated by ';'

#PHASE 4
#matplotlib first sets up the figure contents, than displays it
plt.xlabel("Time [ms]")    #set the X-axis label
plt.ylabel("Loop voltage [V]")    #set the Y-axis label
plt.axhline(color='k')    #draw a black horizontal line as the X-axis
plt.title("Loop voltage evolution")     #set the plot title
plt.ylim(0, data[:,1].max() *1.1)    #set the Y-axis limit from 0 to data maximum + 10% for having some nice space
plt.plot(data[:,0], data[:,1], 'k-', label="Loop voltage") #use the first(0) column as x data, second(1) column as y data,
                #draw with a black(k) line(-)
                #and label this data set Loop voltage to appear in the legend
plt.legend()     #generate the legend from plot labels                
plt.show() #display the figure

Now you can run the script in a command prompt as

user@host$ python2 python_script.py

Or on *NIX systems, the script can be set to be executable and then the program specified on the first line of the script is automatically called to process it

user@host$ chmod +x python_script.py     #set the executable bit
user@host$ python_script.py              #now the script can be run as a stand-alone program

As it now behaves as a program, it should be possible just to simply open in your file manager by (double-)clicking it.

3.1 Command-line parameters

To make the script fit for automatic looping it should be able to process command-line parameters. This is done by changing only a few lines in the above script.

#<--SNIP-->
import sys #import the sys module for access to command-line parameters
shot, data_directory, data_file = sys.argv[1:4] #tuple unpacking of sevral parameters
#starting with second parameter and ending before fourth parameter,
#because first parameter is the name of the script
#python indices start from 0
#<--SNIP-->

The script can now be run in the command prompt as

user@host$ python_script.py 6231 basicdiagn Uloop_dp.csv     #provided the executable bit is set

4 Using the interactive shell prompt

The python interpreter can be also run in an interactive mode by simply running python2 in the command prompt. Then the above code can be directly copied and pasted into the python prompt.

For a much better python prompt we recommend the ipython project.

5 Further resources

Author: Ondrej Grover

Org version 7.5 with Emacs version 23

Validate XHTML 1.0