SlideShare una empresa de Scribd logo
Python plotting for lab folk
Only the stuff you need to know to
 make publishable figures of your
  data. For all else: ask Sourish
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes and axes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
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/english
2e/
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 the
pylab package.
Starting up
I usually open a terminal, and give the command
“ipython –pylab”. Then this appears:




  This may work slightly differently on your computer.
Starting up
Now I can give commands, or run some script
that I have on my computer somewhere.
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.
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
A simple time series plot
In Excel:
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).
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
A simple timeseries plot (step 3)
Read the data from the file and get them into a
tidy 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
A simple timeseries plot (step 4)
Set up the figure, get the values you want to
plot in lists, and plot.




                                        For use in the legend
Formatting string: specifies blue (b)
lines (-) with square (s) markers
A simple timeseries plot (first result)
A simple timeseries plot (step 5)
Format the axes and embellish your plot with
titles, axis labels, legends, annotations. Save.
A simple timeseries plot (end result)
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:
Errorbar plot
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes and axes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
Plot with two y-axes
You can make a plot with two y-axes with the twinx() command:
Plot with two y-axes
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.
Multipane plots
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.
Multipane plots
Multipane plots
Even more customizable subplots can be made with
add_axes(), should you want it.
Multipane plots
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes and axes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
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.
A Keeling plot
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes and axes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
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.
Maps
Maps can be made with the basemap package
Scatter plots and maps
Maps can be combined with other things, like
plot(), errorbar() and scatter().
Overview
•   Introductory stuff
•   A simple time series plot
•   Plots with multiple panes and axes
•   A Keeling plot
•   Scatterplots and maps
•   Functions, modules and classes
Functions
When your script gets longer, it can be a good
idea to group some statements into functions.
“def” starts             The function needs
function definition      this argument
                                                             Optional
                                                             argument




                                                                   Body: what the
                                              Return value         function does




              Calls to the function
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.
Classes
Almost 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

Más contenido relacionado

La actualidad más candente

Matplotlib Review 2021
Matplotlib Review 2021Matplotlib Review 2021
Matplotlib Review 2021Bhaskar J.Roy
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYAMaulik Borsaniya
 
Data Visualization using matplotlib
Data Visualization using matplotlibData Visualization using matplotlib
Data Visualization using matplotlibBruno Gonçalves
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)PyData
 
PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019Ralf Gommers
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologiesPolad Saruxanov
 
Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207Jay Coskey
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysisPramod Toraskar
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friendHoang Nguyen
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Pythonprimeteacher32
 

La actualidad más candente (14)

Matplotlib Review 2021
Matplotlib Review 2021Matplotlib Review 2021
Matplotlib Review 2021
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
Data Visualization using matplotlib
Data Visualization using matplotlibData Visualization using matplotlib
Data Visualization using matplotlib
 
NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019PyData NYC whatsnew NumPy-SciPy 2019
PyData NYC whatsnew NumPy-SciPy 2019
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologies
 
Python 3.6 Features 20161207
Python 3.6 Features 20161207Python 3.6 Features 20161207
Python 3.6 Features 20161207
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysis
 
Python your new best friend
Python your new best friendPython your new best friend
Python your new best friend
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Python
 
Data structures
Data structuresData structures
Data structures
 

Destacado

Calculating the air-sea flux of any trace gas: transfer velocity, chemical e...
Calculating the air-sea flux of any trace gas:  transfer velocity, chemical e...Calculating the air-sea flux of any trace gas:  transfer velocity, chemical e...
Calculating the air-sea flux of any trace gas: transfer velocity, chemical e...Martin Johnson
 
Batenburg caribic seeheim2011
Batenburg caribic seeheim2011Batenburg caribic seeheim2011
Batenburg caribic seeheim2011Anneke Batenburg
 

Destacado (6)

Batenburg agu 2012_11_26
Batenburg agu 2012_11_26Batenburg agu 2012_11_26
Batenburg agu 2012_11_26
 
Vlookup presentation
Vlookup presentationVlookup presentation
Vlookup presentation
 
Calculating the air-sea flux of any trace gas: transfer velocity, chemical e...
Calculating the air-sea flux of any trace gas:  transfer velocity, chemical e...Calculating the air-sea flux of any trace gas:  transfer velocity, chemical e...
Calculating the air-sea flux of any trace gas: transfer velocity, chemical e...
 
Egu2011 2599 Presentation
Egu2011 2599 PresentationEgu2011 2599 Presentation
Egu2011 2599 Presentation
 
Batenburg bbos2011
Batenburg bbos2011Batenburg bbos2011
Batenburg bbos2011
 
Batenburg caribic seeheim2011
Batenburg caribic seeheim2011Batenburg caribic seeheim2011
Batenburg caribic seeheim2011
 

Similar a Python for lab_folk

Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operationsSmee Kaem Chann
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxRohitKumar639388
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...Jose Quesada (hiring)
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python Jhon Valle
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bagJacob Green
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmerGirish Kumar A L
 
Python library
Python libraryPython library
Python libraryToniyaP1
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Reuven Lerner
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxkalai75
 
Python. libraries. modules. and. all.pdf
Python. libraries. modules. and. all.pdfPython. libraries. modules. and. all.pdf
Python. libraries. modules. and. all.pdfprasenjitghosh1998
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending PythonPriyank Kapadia
 

Similar a Python for lab_folk (20)

CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
 
Python training
Python trainingPython training
Python training
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
 
Python
PythonPython
Python
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Python library
Python libraryPython library
Python library
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
Python. libraries. modules. and. all.pdf
Python. libraries. modules. and. all.pdfPython. libraries. modules. and. all.pdf
Python. libraries. modules. and. all.pdf
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 

Último

Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya - UEM Kolkata Quiz Club
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...sanghavirahi2
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/siemaillard
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxCeline George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 

Último (20)

Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 

Python for lab_folk

  • 1. Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish
  • 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. 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/english 2e/
  • 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 the pylab package.
  • 5. Starting up I usually open a terminal, and give the command “ipython –pylab”. Then this appears: This may work slightly differently on your computer.
  • 6. Starting up Now I can give commands, or run some script that I have on my computer somewhere.
  • 7. 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.
  • 8. Overview • Introductory stuff • A simple time series plot • Plots with multiple panes • A Keeling plot • Scatterplots and maps • Functions, modules and classes
  • 9. A simple time series plot In Excel:
  • 10. 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).
  • 11. 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
  • 12. A simple timeseries plot (step 3) Read the data from the file and get them into a tidy 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. A simple timeseries plot (step 4) Set up the figure, get the values you want to plot in lists, and plot. For use in the legend Formatting string: specifies blue (b) lines (-) with square (s) markers
  • 14. A simple timeseries plot (first result)
  • 15. A simple timeseries plot (step 5) Format the axes and embellish your plot with titles, axis labels, legends, annotations. Save.
  • 16. A simple timeseries plot (end result)
  • 17. 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:
  • 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. Plot with two y-axes You can make a plot with two y-axes with the twinx() command:
  • 21. Plot with two y-axes
  • 22. 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.
  • 24. 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.
  • 26. Multipane plots Even more customizable subplots can be made with add_axes(), should you want it.
  • 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. 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.
  • 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. 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.
  • 33. Maps Maps can be made with the basemap package
  • 34. Scatter plots and maps Maps can be combined with other things, like plot(), errorbar() and scatter().
  • 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. Functions When your script gets longer, it can be a good idea to group some statements into functions. “def” starts The function needs function definition this argument Optional argument Body: what the Return value function does Calls to the function
  • 37. 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.
  • 38. Classes Almost 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