Skip to main content

Posts

Showing posts from August, 2015

Lists, Comprehensions, and Generators

In A Student’s Guide to Python for Physical Modeling , we emphasized NumPy arrays and paid less attention to Python lists. The reason is simple: In most scientific computing applications, NumPy arrays store data more efficiently and speed up mathematical calculations, sometimes a thousandfold. However, there are some applications where a Python list is the better choice. There are also times when the choice between a list and an array has little or no effect on performance. In such cases a list can make your code easier to read and understand, and that is always a good thing. In this post, I will describe Python lists and explain a special Python construct for creating lists called a list comprehension . I will also describe a similar construct called a generator expression . Lists A list is an ordered collection of items. You may have made a “To Do” list this morning or a grocery list for a recent trip to the store. In computer science, a list is a data structure that sup

Function Arguments: *args and **kwargs

In Python, functions can have positional arguments and named arguments . In this post, I will describe both types and explain how to use special syntax to simplify repetitive function calls with nearly the same arguments. This extends the discussion in section 5.1.3 of A Student’s Guide to Python for Physical Modeling . First, let’s look at np.savetxt , which has a straightforward declaration: $ import matplotlib.pyplot as plt $ from mpl_toolkits.mplot3d import Axes3D $ np.savetxt? Signature: np.savetxt( fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ') Docstring: Save an array to a text file. We see the function has two required arguments, followed by several optional arguments with default values. Next, let’s look at something more exotic: $ Axes3D.plot_surface? Signature: Axes3D.plot_surface(X, Y, Z, *args, **kwargs) Docstring: Create a surface plot. The f

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