The code for each chapter is available as Python notebook (.ipynb file) and these are available in this folder. They can also be run, from GitHub, on Google Colab by clicking the Colab button below.
These notebooks work for > 3.6.0 and progressively less well for older versions. CoLab is problematic because it provides only 3.2.2 (as of January 2023) and are problems with some multi-stage plots not properly overlaying, and ending up as a stack of individual plots.
Therefore the first code cell in the notebook installs a newer version of Matplotlib. This is an annoying thing to have to do, since then we have to restart the kernel and run the cell again. Until Google updates the default Matplotlib I'm not sure what else to do.
Animations using Matplotlib work well from the Python REPL or scripts, or from IPython (rvctool
).
I have not found a good way to do Matplotlib animations from CoLab. Jupyter itself makes this hard but you can always have windows that pop out of Jupyter, this can't be done from CoLab.
The Jupyter code is annotated where this is an issue, and sometimes alternative appproaches are given instead.
The book assumes that Python results are displayed by default, and use a semicolon on the end of the line to disable the printing. This is like MATLAB and simplifies the examples, print statements are not required. In a Jupyter this automatic printing only applies to the last statement in the cell.
This feature is enabled in the first code cell of the notebook with the code lines:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last_expr_or_assign"
To convert a Jupyter notebook to a Python script use
jupyter nbconvert notebook.ipynb --to python
which creates notebook.py
Other options for --to
include html
, pdf
, markdown
, latex
. See
jupyter nbconvert help
for all the details.