SlideShare a Scribd company logo
Scientific Programming &
Visualisation
Steve Crouch
Software Sustainability Institute
s.crouch@software.ac.uk
Why is Python so popular with researchers?
● Historically, MathWorks MATLAB very popular
○ Been around since 1984
○ Built-in support for efficient numerical matrix analysis, plotting
○ However, it's proprietary commercial software
● Scientific Python trilogy
○ IPython: foundation of the Notebook, created in 2001 by Fernando Perez
○ Matplotlib: plotting library, created in 2003 by John D. Hunter
○ NumPy: fast matrix maths library, created in 2005 by Travis Oliphant
○ All available as a free and open toolchain
○ Also get benefits of general Python language
Introduction to NumPy
● A third-party, non-core Python library
○ Stands for 'Numerical Python'
● The array object in NumPy is called ndarray
○ Can only contain one type - e.g all integers, floats, text strings, etc.
○ Fixed size - editing an ndarray deletes and recreates it each time
○ Similar in some respects to Python, e.g. element slicing
○ Provides many support functions to work with them
● Use additional SciPy package to gain more MATLAB-like functionality
○ Linear algebra, integration, interpolation, FFT, signal/image processing, etc.
NumPy arrays vs Python lists
● Advantages of NumPy arrays
○ Less memory - data structure has denser, smaller memory footprint
○ High performance - often orders of magnitude faster
○ Convenience - supports powerful matrix and scientific operations
○ Similar to arrays in other languages in concept
● Advantages of Python lists
○ Can contain different data types, unlike NumPy
○ It's a core data type - don't need to be explicitly declared
NumPy arrays trade flexibility for performance, efficiency, and power
Introduction to Matplotlib
"The purpose of computing is insight, not numbers"
- Richard Hamming
● Most popular Python data visualisation library
● Very powerful, comprehensive
○ Bar graph, line/pie charts, histograms, box plots, ...
○ Huge customisation options
○ Publication quality
● Easy to get started, can be complicated
○ Single line to generate a graph
Example Rosenbrock function
Image author: Adrien F. Vincent, CC-BY-SA
Anatomy of a Matplotlib plot
● A plot is a hierarchy of objects
● Outermost: Figure object
○ Can contain multiple Axes objects
● Axes object
○ Actually represents a 'plot' or 'graph'
○ Contain many smaller object types
● Smaller object types
○ Tick marks, individual lines, legends,
text boxes, ...
Figure
Axes
...
Example: polar curve graph
Example polar curve graph
Image/code author: Cdang, CC-BY-SA
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0, 2*np.pi, 20)
theta[19] = theta[0]
r = 1 + 0.2*np.random.randn(20)
r[19] = r[0]
plt.polar(theta, r)
plt.savefig("courbe_polaire_rayon_aleatoire.svg",
format="svg")
Learning objectives
Scientific Programming with NumPy
● Explain key similarities and
differences between NumPy arrays
and Python lists
● Select subsets of data from a
NumPy array using slicing
● Efficiently perform elementwise
operations on data
● Obtain characteristics of tabular
data using NumPy's statistical
functions
Data Visualisation with Matplotlib
● Generate a heatmap of longitudinal,
numerical tabular data
● Create graphs of mean, min and max
characteristics over time from data
● Create graphs showing multiple data
characteristics within a single plot and
separate plots
● Save generated graph to local storage
● Write a script to visualise data from
multiple data files

More Related Content

Similar to 2.2-Intro-NumPy-Matplotlib.pptx

Sci computing using python
Sci computing using pythonSci computing using python
Sci computing using python
Ashok Govindarajan
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
yazad dumasia
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
Shubham Yadav
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
UttamKumar617567
 
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
kalai75
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
Nicholas Pringle
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologies
Polad Saruxanov
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
MansiSuthar3
 
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
MLconf
 
10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems
Xavier Amatriain
 
10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf
Xavier Amatriain
 
PYTHON by kunal.pptx
PYTHON by kunal.pptxPYTHON by kunal.pptx
PYTHON by kunal.pptx
kunalGoyal89
 
Data science
Data scienceData science
Data science
Purna Chander
 
Python for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptxPython for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptx
Dr. Amanpreet Kaur
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysis
Pramod Toraskar
 
Artificial Intelligence concepts in a Nutshell
Artificial Intelligence concepts in a NutshellArtificial Intelligence concepts in a Nutshell
Artificial Intelligence concepts in a Nutshell
kannanalagu1
 
Numba: Flexible analytics written in Python with machine-code speeds and avo...
Numba:  Flexible analytics written in Python with machine-code speeds and avo...Numba:  Flexible analytics written in Python with machine-code speeds and avo...
Numba: Flexible analytics written in Python with machine-code speeds and avo...
PyData
 
Five python libraries should know for machine learning
Five python libraries should know for machine learningFive python libraries should know for machine learning
Five python libraries should know for machine learning
Naveen Davis
 
Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"
Fwdays
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
Gaurav Aggarwal
 

Similar to 2.2-Intro-NumPy-Matplotlib.pptx (20)

Sci computing using python
Sci computing using pythonSci computing using python
Sci computing using python
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
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
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
 
Intellectual technologies
Intellectual technologiesIntellectual technologies
Intellectual technologies
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
 
10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems
 
10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf
 
PYTHON by kunal.pptx
PYTHON by kunal.pptxPYTHON by kunal.pptx
PYTHON by kunal.pptx
 
Data science
Data scienceData science
Data science
 
Python for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptxPython for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptx
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysis
 
Artificial Intelligence concepts in a Nutshell
Artificial Intelligence concepts in a NutshellArtificial Intelligence concepts in a Nutshell
Artificial Intelligence concepts in a Nutshell
 
Numba: Flexible analytics written in Python with machine-code speeds and avo...
Numba:  Flexible analytics written in Python with machine-code speeds and avo...Numba:  Flexible analytics written in Python with machine-code speeds and avo...
Numba: Flexible analytics written in Python with machine-code speeds and avo...
 
Five python libraries should know for machine learning
Five python libraries should know for machine learningFive python libraries should know for machine learning
Five python libraries should know for machine learning
 
Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 

Recently uploaded

一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 

Recently uploaded (20)

一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 

2.2-Intro-NumPy-Matplotlib.pptx

  • 1. Scientific Programming & Visualisation Steve Crouch Software Sustainability Institute s.crouch@software.ac.uk
  • 2. Why is Python so popular with researchers? ● Historically, MathWorks MATLAB very popular ○ Been around since 1984 ○ Built-in support for efficient numerical matrix analysis, plotting ○ However, it's proprietary commercial software ● Scientific Python trilogy ○ IPython: foundation of the Notebook, created in 2001 by Fernando Perez ○ Matplotlib: plotting library, created in 2003 by John D. Hunter ○ NumPy: fast matrix maths library, created in 2005 by Travis Oliphant ○ All available as a free and open toolchain ○ Also get benefits of general Python language
  • 3. Introduction to NumPy ● A third-party, non-core Python library ○ Stands for 'Numerical Python' ● The array object in NumPy is called ndarray ○ Can only contain one type - e.g all integers, floats, text strings, etc. ○ Fixed size - editing an ndarray deletes and recreates it each time ○ Similar in some respects to Python, e.g. element slicing ○ Provides many support functions to work with them ● Use additional SciPy package to gain more MATLAB-like functionality ○ Linear algebra, integration, interpolation, FFT, signal/image processing, etc.
  • 4. NumPy arrays vs Python lists ● Advantages of NumPy arrays ○ Less memory - data structure has denser, smaller memory footprint ○ High performance - often orders of magnitude faster ○ Convenience - supports powerful matrix and scientific operations ○ Similar to arrays in other languages in concept ● Advantages of Python lists ○ Can contain different data types, unlike NumPy ○ It's a core data type - don't need to be explicitly declared NumPy arrays trade flexibility for performance, efficiency, and power
  • 5. Introduction to Matplotlib "The purpose of computing is insight, not numbers" - Richard Hamming ● Most popular Python data visualisation library ● Very powerful, comprehensive ○ Bar graph, line/pie charts, histograms, box plots, ... ○ Huge customisation options ○ Publication quality ● Easy to get started, can be complicated ○ Single line to generate a graph Example Rosenbrock function Image author: Adrien F. Vincent, CC-BY-SA
  • 6. Anatomy of a Matplotlib plot ● A plot is a hierarchy of objects ● Outermost: Figure object ○ Can contain multiple Axes objects ● Axes object ○ Actually represents a 'plot' or 'graph' ○ Contain many smaller object types ● Smaller object types ○ Tick marks, individual lines, legends, text boxes, ... Figure Axes ...
  • 7. Example: polar curve graph Example polar curve graph Image/code author: Cdang, CC-BY-SA import numpy as np import matplotlib.pyplot as plt theta = np.linspace(0, 2*np.pi, 20) theta[19] = theta[0] r = 1 + 0.2*np.random.randn(20) r[19] = r[0] plt.polar(theta, r) plt.savefig("courbe_polaire_rayon_aleatoire.svg", format="svg")
  • 8. Learning objectives Scientific Programming with NumPy ● Explain key similarities and differences between NumPy arrays and Python lists ● Select subsets of data from a NumPy array using slicing ● Efficiently perform elementwise operations on data ● Obtain characteristics of tabular data using NumPy's statistical functions Data Visualisation with Matplotlib ● Generate a heatmap of longitudinal, numerical tabular data ● Create graphs of mean, min and max characteristics over time from data ● Create graphs showing multiple data characteristics within a single plot and separate plots ● Save generated graph to local storage ● Write a script to visualise data from multiple data files

Editor's Notes

  1. Welcome to the Scientific Programming and Visualisation session, where we'll look at two tools that can help us manipulate numerical data and generate graphs. We'll first be looking at how a specialised library called NumPy can help us to perform numerical operations on matrix-based data efficiently and quickly. We'll also be using another library, Matplotlib, to generate graphs that yield insight into this data.
  2. So why is Python so popular with researchers? Historically, a software product called MATLAB has proved very popular with researchers. Firstly, it's been around for a long time - it's a very mature piece of software with excellent integration between it's many capabilities, and has excellent documentation Primarily for numerical computing, it support things like numerical matrix analysis and manipulation, plotting of functions and data However, a big drawback is that it's proprietary commercial software, so you need to buy a licence to use it But then, a set of three Python packages began surfacing after the turn of the millenium IPython, foundation for the Jupyter Notebook, created in 2001 by Fernando Perez Matplotlib, a library for creating graphical plots, created in 2003 by John D. Hunter And also NumPy, which arrived in 2005. A fast matrix mathematics library, created by Travis Oliphant Now these are all open source and free, have been developed and supported for over a decade, function well together, and have been gaining traction with the academic community ever since. And you also get benefits of general Python language - it's flexibility and general purpose nature mean you can make use of features from these packages in larger projects where you need them, and you aren't tied to only the single package, like MATLAB.
  3. So what is NumPy? It stands for Numerical Python, And it's a third party Python library, which means it doesn't come as part of Python as it's normally installed. So we need to install it as a separate Python package. The core thing NumPy provides you is the ndarray. The ndarray is an array of objects - an array is a data structure which can store a collection of ordered elements of the same type of data So all elements in an array, or ndarray, must be of a single type, so all integers, all floats, text strings, and so on Since ndarrays are of fixed size, unlike Python lists, every time we change the contents of an ndarray we are deleting it and recreating it each time In some respects, they act like Python lists, for example you slice an ndarray in the same way. But in other ways they are very different. But these differences give us some key advantages ndarrays also come with many support functions and capabilities for performing operations on them at a high-level which is very useful There is also an additional package called SciPy which makes use of NumPy arrays to provide more MATLAB-like functionality Such as linear algebra
  4. Let's take a look at the core array object provide by NumPy, in relation to the Python list. So what are the advantages of NumPy arrays? Firstly, they consume significantly less memory than Python lists Because NumPy arrays are of only one type and of fixed size they can be stored in a more densely-packed format which is much more efficient NumPy arrays are also high performance Tasks executed in NumPy are around 5 to 100 times faster than lists This is because parts requiring fast computation are written in C/C++, and also in part, because of how they're stored in memory, it's more efficient to process them And also, NumPy is optimised to work with the latest CPU architectures However, it's important to bear in mind that Python lists still have advantages which may steer you in their direction: They can contain different data types, unlike NumPy, so are far more flexible They are part of the core library, and don't need to be explicitly declared as a specific type, unlike ndarrays So in the end, NumPy arrays trade flexibility you have with Python lists for performance, efficiency, and also power - due to how it's designed around the ndarray, you can perform complex and powerful operations on them using very little code. If you don't need the flexibility offered by Python lists, but are doing a lot of intensive operations over single of type data, and value efficiency and performance, use NumPy arrays.
  5. The mathematician Richard Hamming once said, “The purpose of computing is insight, not numbers,” and the best way to develop insight is often to visualize data. Visualization deserves an entire lecture of its own, but we can explore a few features of Python’s matplotlib library here. While there is no official plotting library, matplotlib is the de facto standard. Matplotlib is very powerful and also comprehensive With it, you can create charts of many types, including basic bar graphs, line or pie charts, histograms, box plots, scatter plots. It also supports 3D graphs, as you can see from the example Rosenbrock function graph on the right It allows for massive customisation of graphs too - you can add or change graph labels, axis limits, colours, background grid, pretty much anything You can also generate publication quality graphs, with some work - see the example Rosenbrock function [a popular test problem for gradient-based optimization algorithms] It's also easy to get started - you can generate a graph of a specific type with a single line. But you can also go a lot further and produce very complex, tailored graphs using Matplotlib's API However, it can be confusing - the library itself is around 70,000 lines of code, and there are several different ways of constructing a figure. Also note that it is still evolving, and Matplotlib's own documentation is often out-of-date
  6. As we've said, Matplotlib can get a little confusing, So it's useful to know how Matplotlib puts a graph or plot together. A plot you generate is made of a hierarchy of objects: The outermost object is the Figure object. This can contain multiple Axes objects. Which could be side by side, vertical, arranged in a grid, or even on top of each other. The Axes objects are not actually to do with the axis concept of a graph. These actually represent an individual plot or graph as we would know it Each Axes object can contain many other smaller object types which make up the graph itself, such as: tick marks, individual lines, legends, text boxes, labels, and so on So now we have a conceptual overview of what a Matplotlib plot is, let's look at an actual Python example.
  7. Let's put NumPy and Matplotlib together, using some example Python which is used to generate the polar graph on the top right. For this example We first import the numpy library as well as Matplotlib Then we use NumPy to generate a NumPy array filled with evenly spaced numbers over a specified interval (using linspace, a function within NumPy) - these form the polar x coordinates for our graph We then generate some random numbers with a common coefficient and constant - these form the polar y coordinates for our graph By passing these x and y values to the polar() function of Matplotlib, we generate a new figure - our polar curve graph We then save that Figure as a file An important thing to note is that here, we're making use of hidden Matplotlib PyPlot state. Whilst plt is what we use to refer to the Matplotlib PyPlot module as a convenient shorthand, plt.polar() is implicitly creating a new Figure which is held internally by Matplotlib. And plt.savefig() is referring to that hidden Figure - in order to save it as a file So - it's good to remember that Plt - or PyPlot - is just a set of wrappers around Matplotlib's lower-level API, and there this hidden Figure state held in the background is what we are creating and changing as we make calls via plt Which implies there is only ever one Figure that you're manipulating at any given time So, throughout this introduction, we've taken a look at two key libraries that can greatly help numerical analysis and visualisation, and the reasons to use them. So the next thing to do is to make a start on the self-learning online materials, which aims to give you some hands-on experience using these libraries, making use of a medical inflammation dataset to introduce you to the features of NumPy and Matplotlib which you can use to analyse data and visualise it, as aids to give you insight to the properties of that data.
  8. So what are the learning objectives for this session? With NumPy, be able to: Explain the key similarities and differences between NumPy arrays and Python lists Select subsets of data from a NumPy array using slicing Efficiently perform elementwise operations on data And obtain characteristics of tabular data using NumPy's statistical functions In terms of Matplotlib, be able to: Firstly generate a heatmap of longitudinal and numerical tabular data Create graphs of mean, minimum, and maximum characteristics over time from data Also create graphs to show multiple data characteristics within a single plot and within separate plots Save a generated graph to local file storage Write a script to visualise data from multiple data files