Skip to main content

Contents

On this page, we attempt to maintain an annotated directory of the content of this blog.

Pages

There are 6 static pages in this blog:

Blog Posts

The list of posts, in the order in which they were published to the blog:

  1. Welcome! — An introduction to the blog.

  2. Making Plots for Publication — We discuss some tips and tricks for adjusting plots generated by PyPlot in order to meet the style requirements of some journals. We also provide a module that automates some of these adjustments.

  3. Python Code in LaTeX — We explain how to produce Python code with syntax highlight in LaTeX documents. We also provide style files that will reproduce the appearance of code samples in A Student’s Guide to Python for Physical Modeling.

  4. Interpolation — We explain the basics of interpolation (estimating the value of a function between sampled points) and describe how to use the interp1d function from the scipy.interpolate module.

  5. Raising a Figure Window to the Foreground — We describe how to force a figure window to the foreground of your operating system using either the Qt or Tkinter graphics backend. (Plots created by Spyder are sometiems hard to locate.)

  6. Displaying Plots Inside Loops — We explain how to display figures during the execution of a loop using PyPlot’s waitforbuttonpress() method. Python’s default behavior is to display nothing until the loop exits, regardless of how many figures you create.

  7. Illuminating Surface Plots — We describe how to use the LightSource class from the matplotlib.colors module to control the illumination and shading of three-dimensional surface plots.

  8. Function Arguments: *args and **kwargs — We describe positional and named arguments in Python functions, the wildcard arguments *args and **kwargs, and how to use this notation to pass arguments to functions.

  9. Lists, Comprehensions, and Generators — We describe Python lists, a special Python construct for creating lists called a list comprehension, and a similar construct called a generator expression.

  10. Speeding Up Python — Part 1: Profiling — This is the first of a two-part discussion of profiling and optimization in Python. We explain what profiling is, introduce several command-line tools for profiling Python programs, and provide some tips for effective profiling.

  11. Speeding Up Python — Part 2: Optimization — This is the second of a two-part discussion of profiling and optimization in Python. We use these tools described in the previous post to demonstrate some general principles that improve the performance of Python programs.

  12. Paths in Python — We discuss how you can manage the collection of directories searched by Python. This makes it simple to access modules you write or download in other Python programs.

  13. Jupyter Notebooks — We introduce Jupyter notebooks with a working example. A Jupyter notebook is an interactive document that incorporates text, math, graphics, and code. This make Jupyter notebooks very useful for presenting your work or sharing it with others.

  14. A Few Updates — We describe a few significant updates to Python and the Anaconda distribution of Python since the publication of the first edition of A Student’s Guide to Python for Physical Modeling in 2015. We also discuss our upcoming revised edition of the book.

Comments

Popular posts from this blog

Paths in Python

How do you get your Python interpreter to find modules that are not located in your current working directory? The answer is … you tell it where to look. When you type something like from my_module import my_function Python searches a collection of directories (i.e., folders) on your computer. If the directory containing <my_module.py> is not in this collection, you will receive an ImportError . This can be frustrating if you know the file exists, and even more so if you know where it exists. In this post, we will take a brief look at how to add paths to the collection of directories searched by Python. Paths A path is essentially a set of directions to a file: /Users/username/modules/my_module.py [Mac OS X, Linux] C:\modules\my_module.py [Windows] It tells your operating system how to navigate from a fixed starting point — the “root directory” / in Unix-based systems, or C:\ in Windows — through a collection of folders to the de

Illuminating Surface Plots

Matplotlib provides functions for visualizing three-dimensional data sets. One useful tool is a surface plot. A surface plot is a two-dimensional projection of a three-dimensional object. Much like a sketch artist, Python uses techniques like perspective and shading to give the illusion of a three-dimensional object in space. In this post, I describe how you can control the lighting of a surface plot. Surface Plots First, let’s look at some of the options available with the default three-dimensional plotting tools. This script will create a surface plot of a Bessel function. Its ripples will emphasize the effects of lighting later. import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # Import 3D plotting tools. from scipy.special import jn # Import Bessel function. # Define grid of points. points = np.linspace(-10, 10, 51) X, Y = np.meshgrid(points, points) R = np.sqrt(X**2 + Y**2) Z = jn(0,R) # Create 3D surface plo

Raising a Figure Window to the Foreground

This post describes a utility function that will raise a plot window to the foreground of your screen. The function will only work with the Qt graphics backend, so I will start with a brief overview of graphics backends. If you just want to use the function as a black box, you can do the following: Set the Graphics Backend to “Qt” in the Spyder preferences menu. Copy this function into your working directory. Graphics Backends You may have have written a script to produce precisely the data you need, but a lot of processing is required to transform these numbers into a figure. You need to create a plot window, draw a figure inside of it, and manage all of the attributes that control a figure’s appearance: title, axis labels, line widths, colors, tick marks, etc. All of this happens in the background when you type a command like plt.plot(x,y) . A graphics backend is the software that Python uses to physically draw a figure on your computer screen. If you have already import