Python plotting for lab folkOnly the stuff you need to know to make publishable figures of your  data. For all else: ask S...
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes and axes•   A Keeling plot•   Sca...
What is Python?• Python is an general-purpose high-level programming  language• Many, many things are done with Python• Th...
Packages for plotting data• Matplotlib: for plotting                (  http://matplotlib.sourceforge.net/, you’ll  need th...
Starting upI usually open a terminal, and give the command“ipython –pylab”. Then this appears:  This may work slightly dif...
Starting upNow I can give commands, or run some scriptthat I have on my computer somewhere.
ScriptsScripts are text files with extension ‘.py’ thatcontain Python commands. You can edit them inKomodo or any other te...
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes•   A Keeling plot•   Scatterplots...
A simple time series plotIn Excel:
A simple timeseries plot (step 1)Save your data in a “clean” Windows Comma SeparatedValue (.csv) file (other text formats ...
A simple timeseries plot (step 2)Start scripting! First, load the useful packages. Maybeset some default settings for the ...
A simple timeseries plot (step 3)Read the data from the file and get them into atidy nested list.                         ...
A simple timeseries plot (step 4)Set up the figure, get the values you want toplot in lists, and plot.                    ...
A simple timeseries plot (first result)
A simple timeseries plot (step 5)Format the axes and embellish your plot withtitles, axis labels, legends, annotations. Sa...
A simple timeseries plot (end result)
Errorbar plotSuppose you want to have errorbars in your plot that are 2%of the values. Then you can replace the plot comma...
Errorbar plot
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes and axes•   A Keeling plot•   Sca...
Plot with two y-axesYou can make a plot with two y-axes with the twinx() command:
Plot with two y-axes
Multipane plotsThe simplest way to define subplots is with the subplot() orfig.add_subplot() commands. In the brackets sho...
Multipane plots
Multipane plotsThe distance between the subplots is adjustable, alsoto 0. The NullFormatter() can be used to remove theaxi...
Multipane plots
Multipane plotsEven more customizable subplots can be made withadd_axes(), should you want it.
Multipane plots
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes and axes•   A Keeling plot•   Sca...
A Keeling plotPython offers more possibilities than Excel forcustomized fits to data. There arescipy.stats.linregress() an...
A Keeling plot
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes and axes•   A Keeling plot•   Sca...
Scatter plotsOf all the other plot possibilities that matplotlib offers, Ifind the scatter plots quite useful.In scatter p...
MapsMaps can be made with the basemap package
Scatter plots and mapsMaps can be combined with other things, likeplot(), errorbar() and scatter().
Overview•   Introductory stuff•   A simple time series plot•   Plots with multiple panes and axes•   A Keeling plot•   Sca...
FunctionsWhen your script gets longer, it can be a goodidea to group some statements into functions.“def” starts          ...
ModulesFunction definitions can be grouped into a fileand then imported into a script (orinteractively). Such a file with ...
ClassesAlmost everything in Python is an object of some class or other.Object classes have “methods” associated with them ...
Upcoming SlideShare
Loading in …5
×

Python for lab_folk

743 views

Published on

Presentation I threw together for my lab mates as a practical introduction to scientific data plotting with Python.

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
743
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
13
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Python for lab_folk

  1. 1. Python plotting for lab folkOnly the stuff you need to know to make publishable figures of your data. For all else: ask Sourish
  2. 2. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes and axes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  3. 3. 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 onhttp://docs.python.org/tutorial/. Very useful!This one also seems useful:http://www.openbookproject.net/thinkcs/python/english2e/
  4. 4. 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_Tuto rial )These libraries/packages are combined in thepylab package.
  5. 5. Starting upI usually open a terminal, and give the command“ipython –pylab”. Then this appears: This may work slightly differently on your computer.
  6. 6. Starting upNow I can give commands, or run some scriptthat I have on my computer somewhere.
  7. 7. ScriptsScripts are text files with extension ‘.py’ thatcontain Python commands. You can edit them inKomodo or any other text editor that you findconvenient.In principle, you could build your figure bytyping all your commands in the terminal, butthat is really tedious...So from now on, I’ll assume that you want ascript that draws your figure.
  8. 8. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  9. 9. A simple time series plotIn Excel:
  10. 10. A simple timeseries plot (step 1)Save your data in a “clean” Windows Comma SeparatedValue (.csv) file (other text formats are alsopossible, but this usually works best).
  11. 11. A simple timeseries plot (step 2)Start scripting! First, load the useful packages. Maybeset some default settings for the graphics as well. There are different ways to import functions
  12. 12. A simple timeseries plot (step 3)Read the data from the file and get them into atidy nested list. Object-oriented way of calling a function “List comprehensions”: typical for Python and essentially a way to write a list-creating loop very compactly
  13. 13. A simple timeseries plot (step 4)Set up the figure, get the values you want toplot in lists, and plot. For use in the legendFormatting string: specifies blue (b)lines (-) with square (s) markers
  14. 14. A simple timeseries plot (first result)
  15. 15. A simple timeseries plot (step 5)Format the axes and embellish your plot withtitles, axis labels, legends, annotations. Save.
  16. 16. A simple timeseries plot (end result)
  17. 17. Errorbar plotSuppose 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:
  18. 18. Errorbar plot
  19. 19. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes and axes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  20. 20. Plot with two y-axesYou can make a plot with two y-axes with the twinx() command:
  21. 21. Plot with two y-axes
  22. 22. Multipane plotsThe simplest way to define subplots is with the subplot() orfig.add_subplot() commands. In the brackets should be the desirednumber of rows, columns and the number of the figure.
  23. 23. Multipane plots
  24. 24. Multipane plotsThe distance between the subplots is adjustable, alsoto 0. The NullFormatter() can be used to remove theaxis ticklabels. Overlapping ticklabels can be removed.
  25. 25. Multipane plots
  26. 26. Multipane plotsEven more customizable subplots can be made withadd_axes(), should you want it.
  27. 27. Multipane plots
  28. 28. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes and axes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  29. 29. A Keeling plotPython offers more possibilities than Excel forcustomized fits to data. There arescipy.stats.linregress() andscipy.optimize.curvefit(), but you can also writeyour own routines.I often use a home-made bivariate fit modulebased on Cantrell (2008) to fit straight lines todata with errors in x and y, like in Keeling plots.
  30. 30. A Keeling plot
  31. 31. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes and axes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  32. 32. Scatter plotsOf all the other plot possibilities that matplotlib offers, Ifind the scatter plots quite useful.In scatter plots, marker color and/or size can depend ona third variable.
  33. 33. MapsMaps can be made with the basemap package
  34. 34. Scatter plots and mapsMaps can be combined with other things, likeplot(), errorbar() and scatter().
  35. 35. Overview• Introductory stuff• A simple time series plot• Plots with multiple panes and axes• A Keeling plot• Scatterplots and maps• Functions, modules and classes
  36. 36. FunctionsWhen your script gets longer, it can be a goodidea to group some statements into functions.“def” starts The function needsfunction definition this argument Optional argument Body: what the Return value function does Calls to the function
  37. 37. ModulesFunction definitions can be grouped into a fileand then imported into a script (orinteractively). Such a file with definitions iscalled a module.The bivariate fit module that was imported tothe Keeling plot script is an example.If you’re changing your module while runningyour script, you may have to use the reload()command.
  38. 38. ClassesAlmost everything in Python is an object of some class or other.Object classes have “methods” associated with them that canwork 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

×