Python for lab_folk

38
Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish

Transcript of Python for lab_folk

Page 1: Python for lab_folk

Python plotting for lab folk

Only the stuff you need to know to make publishable figures of your

data. For all else: ask Sourish

Page 2: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes and axes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 3: Python for lab_folk

What is Python?

• Python is an general-purpose high-level programming language

• Many, many things are done with Python

• There are many libraries available with modules for specialized tasks. Like for scientific data plotting...

A general tutorial to Python is available on http://docs.python.org/tutorial/. Very useful!

This one also seems useful: http://www.openbookproject.net/thinkcs/python/english2e/

Page 4: Python for lab_folk

Packages for plotting data

• Matplotlib: for plotting ( http://matplotlib.sourceforge.net/, you’ll need this webpage often... )

• Numpy: for scientific computing (http://www.scipy.org/Tentative_NumPy_Tutorial )

These libraries/packages are combined in the pylab package.

Page 5: Python for lab_folk

Starting up

I usually open a terminal, and give the command

“ipython –pylab”. Then this appears:

This may work slightly differently on your computer.

Page 6: Python for lab_folk

Starting upNow I can give commands, or run some script that I have on my computer somewhere.

Page 7: Python for lab_folk

Scripts

Scripts are text files with extension ‘.py’ that contain Python commands. You can edit them in Komodo or any other text editor that you find convenient.

In principle, you could build your figure by typing all your commands in the terminal, but that is really tedious...

So from now on, I’ll assume that you want a script that draws your figure.

Page 8: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 9: Python for lab_folk

A simple time series plot

In Excel:

Page 10: Python for lab_folk

A simple timeseries plot (step 1)Save your data in a “clean” Windows Comma Separated Value (.csv) file (other text formats are also possible, but this usually works best).

Page 11: Python for lab_folk

A simple timeseries plot (step 2)

Start scripting! First, load the useful packages. Maybe set some default settings for the graphics as well.

There are different ways to import functions

Page 12: Python for lab_folk

A simple timeseries plot (step 3)

Read the data from the file and get them into a tidy nested list.

“List comprehensions”: typical for Python and essentially a way to write a list-creating loop very compactly

Object-oriented way of calling a function

Page 13: Python for lab_folk

A simple timeseries plot (step 4)Set up the figure, get the values you want to plot in lists, and plot.

Formatting string: specifies blue (b) lines (-) with square (s) markers

For use in the legend

Page 14: Python for lab_folk

A simple timeseries plot (first result)

Page 15: Python for lab_folk

A simple timeseries plot (step 5)

Format the axes and embellish your plot with titles, axis labels, legends, annotations. Save.

Page 16: Python for lab_folk

A simple timeseries plot (end result)

Page 17: Python for lab_folk

Errorbar plot

Suppose you want to have errorbars in your plot that are 2% of the values. Then you can replace the plot command:

With this command that uses the errorbar function:

Page 18: Python for lab_folk

Errorbar plot

Page 19: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes and axes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 20: Python for lab_folk

Plot with two y-axes

You can make a plot with two y-axes with the twinx() command:

Page 21: Python for lab_folk

Plot with two y-axes

Page 22: Python for lab_folk

Multipane plots

The simplest way to define subplots is with the subplot() or fig.add_subplot() commands. In the brackets should be the desired number of rows, columns and the number of the figure.

Page 23: Python for lab_folk

Multipane plots

Page 24: Python for lab_folk

Multipane plots

The distance between the subplots is adjustable, also to 0. The NullFormatter() can be used to remove the axis ticklabels. Overlapping ticklabels can be removed.

Page 25: Python for lab_folk

Multipane plots

Page 26: Python for lab_folk

Multipane plots

Even more customizable subplots can be made with add_axes(), should you want it.

Page 27: Python for lab_folk

Multipane plots

Page 28: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes and axes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 29: Python for lab_folk

A Keeling plot

Python offers more possibilities than Excel for customized fits to data. There are scipy.stats.linregress() and scipy.optimize.curvefit(), but you can also write your own routines.

I often use a home-made bivariate fit module based on Cantrell (2008) to fit straight lines to data with errors in x and y, like in Keeling plots.

Page 30: Python for lab_folk

A Keeling plot

Page 31: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes and axes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 32: Python for lab_folk

Scatter plots

Of all the other plot possibilities that matplotlib offers, I find the scatter plots quite useful.

In scatter plots, marker color and/or size can depend on a third variable.

Page 33: Python for lab_folk

Maps

Maps can be made with the basemap package

Page 34: Python for lab_folk

Scatter plots and maps

Maps can be combined with other things, like plot(), errorbar() and scatter().

Page 35: Python for lab_folk

Overview

• Introductory stuff

• A simple time series plot

• Plots with multiple panes and axes

• A Keeling plot

• Scatterplots and maps

• Functions, modules and classes

Page 36: Python for lab_folk

Functions

When your script gets longer, it can be a good idea to group some statements into functions.

“def” starts function definition

The function needs this argument

Optional argument

Body: what the function does

Calls to the function

Return value

Page 37: Python for lab_folk

Modules

Function definitions can be grouped into a file and then imported into a script (or interactively). Such a file with definitions is called a module.

The bivariate fit module that was imported to the Keeling plot script is an example.

If you’re changing your module while running your script, you may have to use the reload() command.

Page 38: Python for lab_folk

ClassesAlmost everything in Python is an object of some class or other. Object classes have “methods” associated with them that can work on those objects.

You can define your own object classes and methods.

Method: definition of a function that can work on your object

Another method