Skip to main content

Posts

Showing posts from June, 2015

Interpolation

“I have experimental data for absorption (dependent variable) measured at various wavelengths (independent variable). Now I want estimates of the absorption at other values in the same range.” This is an interpolation problem. We wish to obtain the value of a dependent value at point for which we did not sample the independent value. Interpolation is a mathematical procedure for filling in the gaps between available values. SciPy provides a module for interpolation based on the FITPACK library of FORTRAN functions. (Thus, it is fast and reliable.) A Simple Example To gain access to the interpolation functions, import the module: import scipy.interpolate Or, simply import the function we need for our one-dimensional problem: from scipy.interpolate import interp1d Suppose that each column of an array expData contains a (wavelength, absorption) pair. We can use interp1d to create a function-like object for interpolation from this data set: F = interp1d(expDa...

Python Code in LaTeX

In A Student’s Guide to Python for Physical Modeling , we used the Listings package for LaTeX to format our code samples. In this post, we describe how we used the package and provide our style files for others to use. Typesetting Computer Code The listings package makes it easy to format code for typesetting in LaTeX. It allows the author to adjust virtually every aspect of how code is displayed. We used the package to develop our own syntax highlighting scheme. All code is displayed in a monospace (typewriter) font. Built-in functions and reserved words are bold and green. Python exceptions are bold and red. Modules and functions from NumPy and PyPlot are bold and black. Modules and functions from other modules are bold and blue. Strings are red. Keyword arguments to functions are italic. Comments are blue italic. This scheme is similar to the one used in Spyder. It allows a reader to distinguish built-in functions and modules from variable names and user-defined functi...

Making Plots for Publication

Overview This document extends the discussion of plots and graphs given in A Student’s Guide to Python for Physical Modeling . Here we give some recipes that work on our installation. If they don’t work on yours, please comment on that, especially if you find a workaround. This post describes the following: Setting figure size Importing the SGPniceGraphics module Functions and parameters defined in the module Fixing crowded graphs Saving graphs for modification in other art software Typesetting labels with LaTeX Other rc settings CMYK color Exporting raster images Setting Figure Size You can create a figure window of a specific size before plotting with the command myfigsize = (6, 3) plt.figure(frameon=False, figsize=myfigsize) The first keyword argument suppresses the colored panel in the back of the figure, so you won’t need to delete it by hand in some drawing software. The second keyword argument specifies the desired width and height of t...