7. Numerical and Scientific Python and Data Visualisation

«  6. Writing and Debugging Python Code   ::   Contents   ::   8. Random Digital Elevation Model  »

7. Numerical and Scientific Python and Data Visualisation

We have previously seen some of the main examples of the core data types in Python: strings for text, integer and float for numerical representation; boolean for logical representation; and dictionaries and lists as ways of grouping sets of (potentially different) data types together.

Very often in scientific programming, we wish to perform operations on sets containing the same data type, for instance vectors or matrices of floating point numbers. Of course we can use dictionaries or lists to do this, but they lack some of the general functionality we would normally need. The main package in Python for dealing with numerical manipulation then, is numpy which is often used together with scipy. There are many resources built around these packages, and many tutorials available, such as the one from scipy for numpy and the scipy cookbook. A complete course in Scientific Python is also available. It is up to you to make use of these various resources to back up what we cover in these classes, for further learning, and for general reference.

We strongly recommend that you go through some of these in your own time. At the very least, be aware that there are such resources to help you develop what you can do with these (and other) packages.

Tools to enable you to visualise data are available in a number of packages, but one of the most common ones is matplotlib or the slightly different philosophy of pylab. These have very good help pages, but to start with, you might find the gallery of different examples most useful.

Anyone who has come across matlab before will notice a good deal of similarity between matplotlib (and indeed, elements of numpy) and matlab. Some of the advantages of using the Python packages over matlab are given here but there is much debate around these things and of course there are many tools developed around matlab that you may wish to use at some point. One key advantage of the Python packages over matlab for many users is that it is open source, but another viable open source toolset for many of the things we will do here is R. Rather than attempting to ‘teach’ you all of these tools and packages, it is preferable that we settle on one main one (Python and its packages here) and provide you pointers to how you might use what you learn here in other languages/tools. On the subject of R, we note here that there is a Python interface to R called RPy.

To help appreciate some of the similarities and differences, and to help you transliterate between one language and another, there are several web resources that you might find useful, including NumPy for Matlab Users, A ‘crib sheet’ covering basic equivalences between matlab/octave, python and R is available as a pdf , or a more general ‘Mathesaurus’ provided here.

An additional piece of software to consider and keep an eye on as it develops is sage which is a general open source mathematics software, with a Python-based interface.

7.1. In this session

In this session then, we will not attempt to take you through all of the features of scipy, numpy and matplotlib. Instead, we will develop some examples where we build simple environmental models that you can use.

All of the above is, in one sense just context. What you want to be able to do is get on and write some useful code so that you can explore some interesting data or model. With that in mind, we will now develop some code for a simple environmental model.

«  6. Writing and Debugging Python Code   ::   Contents   ::   8. Random Digital Elevation Model  »