Introduction To Python For Science & Engineering: David J. Pine
Introduction To Python For Science & Engineering: David J. Pine
Pine
Preface xi
1 Introduction 1
1.1 Introduction to Python for Science and Engineering . 1
2 Launching Python 3
2.1 Interacting with Python . . . . . . . . . . . . . . . . . 3
2.2 Installing Python on Your Computer . . . . . . . . . . 4
2.3 The Spyder Window . . . . . . . . . . . . . . . . . . . 4
2.4 The IPython Pane . . . . . . . . . . . . . . . . . . . . . 4
2.4.1 Magic commands . . . . . . . . . . . . . . . . . 6
2.4.2 System shell commands . . . . . . . . . . . . . 8
2.4.3 Tab completion . . . . . . . . . . . . . . . . . . 8
2.4.4 Recap of commands . . . . . . . . . . . . . . . . 9
2.5 Interactive Python as a Calculator . . . . . . . . . . . 9
2.5.1 Binary arithmetic operations in Python . . . . . 10
2.5.2 Types of numbers . . . . . . . . . . . . . . . . . 10
2.5.3 Important note on integer division in Python . 12
2.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.6.1 Names and the assignment operator . . . . . . 13
2.6.2 Legal and recommended variable names . . . . 14
2.6.3 Reserved words in Python . . . . . . . . . . . . 15
2.7 Script Files and Programs . . . . . . . . . . . . . . . . 16
2.7.1 First scripting example: The Editor pane . . . . 16
2.8 Python Modules . . . . . . . . . . . . . . . . . . . . . . 18
2.8.1 Python modules and functions: A first look . . 20
2.8.2 Some NumPy functions . . . . . . . . . . . . . . 22
2.8.3 Scripting Example 2 . . . . . . . . . . . . . . . 23
2.8.4 Different ways of importing modules . . . . . . 24
2.9 Getting Help: Documentation in IPython . . . . . . . 26
iii
iv Contents
6 Plotting 99
6.1 An Interactive Session with PyPlot . . . . . . . . . . . 100
6.2 Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . 102
6.2.1 Specifying line and symbol types and colors . . 106
6.2.2 Error bars . . . . . . . . . . . . . . . . . . . . . 108
6.2.3 Setting plotting limits and excluding data . . . 110
6.2.4 Subplots . . . . . . . . . . . . . . . . . . . . . . 113
6.3 Logarithmic Plots . . . . . . . . . . . . . . . . . . . . . 116
6.3.1 Semi-log plots . . . . . . . . . . . . . . . . . . . 116
6.3.2 Log-log plots . . . . . . . . . . . . . . . . . . . . 118
6.4 More Advanced Graphical Output . . . . . . . . . . . 118
6.4.1 An alternative syntax for a grid of plots . . . . 122
6.5 Plots with multiple axes . . . . . . . . . . . . . . . . . 125
6.6 Mathematics and Greek symbols . . . . . . . . . . . . 126
6.7 The Structure of matplotlib: OOP and All That . . . . 131
6.7.1 The backend layer . . . . . . . . . . . . . . . . . 132
6.7.2 The artist layer . . . . . . . . . . . . . . . . . . . 135
6.7.3 The PyPlot (scripting) layer . . . . . . . . . . . 137
6.8 Contour and Vector Field Plots . . . . . . . . . . . . . 139
6.8.1 Making a 2D grid of points . . . . . . . . . . . . 139
6.8.2 Contour plots . . . . . . . . . . . . . . . . . . . 140
6.8.3 Streamline plots . . . . . . . . . . . . . . . . . . 144
6.9 Three-Dimensional Plots . . . . . . . . . . . . . . . . . 149
6.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 152
7 Functions 155
7.1 User-Defined Functions . . . . . . . . . . . . . . . . . 156
7.1.1 Looping over arrays in user-defined functions . 158
vi Contents
11 Animation 287
11.1 Animating a Sequence of Images . . . . . . . . . . . . 287
11.1.1 Simple image sequence . . . . . . . . . . . . . . 288
11.1.2 Annotating and embellishing videos . . . . . . 292
11.2 Animating Functions . . . . . . . . . . . . . . . . . . . 294
11.2.1 Animating for a fixed number of frames . . . . 295
11.2.2 Animating until a condition is met . . . . . . . 300
11.3 Combining Videos with Animated Functions . . . . . 306
viii Contents
C Glossary 355
Contents ix
Index 363
Preface
xi
xii Introduction to Python for Science & Engineering
David Pine has taught physics and chemical engineering for over 30
years at four different institutions: Cornell University (as a graduate
student), Haverford College, UCSB, and, at NYU, where he is a Profes-
sor of Physics, Mathematics, and Chemical & Biomolecular Engineer-
ing. He has taught a broad spectrum of courses, including numerical
methods. He does research in experimental soft-matter physics, which
is concerned with materials such as polymers, emulsions, and col-
loids. These materials constitute most of the material building blocks
of biological organisms.
xv
chapter 1
Introduction
1
2 Introduction to Python for Science & Engineering
This also means that you can use Python without being tethered to
the internet, as required for commercial software that is tied to a re-
mote license server.
Another advantage is Python’s clean and simple syntax, including
its implementation of object-oriented programming. This should not
be discounted; Python’s rich and elegant syntax renders a number of
tasks that are difficult or arcane in other languages either simpler or
more understandable in Python.
An important disadvantage is that Python programs can be slower
than compiled languages like C. For large-scale simulations and other
demanding applications, there can be a considerable speed penalty in
using Python. In these cases, C, C++, or Fortran is recommended, al-
though intelligent use of Python’s array processing tools contained in
the NumPy module can greatly speed up Python code. Another dis-
advantage is that, compared to MATLAB® , Python is less well docu-
mented. This stems from the fact that it is public open source software
and thus is dependent on volunteers from the community of develop-
ers and users for documentation. The documentation is freely avail-
able on the web but is scattered among a number of different sites and
can be terse. This manual will acquaint you with the most commonly
used web sites. Search engines like Google can help you find others.
You are not assumed to have had any previous programming ex-
perience. However, the purpose of this manual isn’t to teach you the
principles of computer programming; it’s to provide a very practical
guide to getting started with Python for scientific computing. Perhaps
once you see some of the powerful tasks that you can accomplish with
Python, you will be inspired to study computational science and en-
gineering, as well as computer programming, in greater depth.