Skip to main content

Posts

Showing posts from July, 2015

Displaying Plots Inside Loops

In the early stages of some programming projects, I often like to explore the problem visually: to run a series of simulations with different values of the input parameters and create a series of graphs in order to gain some intuition about the problem. A convenient approach is to embed some plotting commands in a loop. For example, suppose I want to see what the first ten Bessel functions look like. It seems that the following script should do what I want: import numpy as np import matplotlib.pyplot as plt from scipy.special import jn # Import Bessel function. r = np.linspace(0,20,101) for n in range(10): plt.plot(r, jn(n,r)) # Draw nth Bessel function. plt.title("Bessel function J[%d](r)." % n) input("Press <Enter> to continue.") # Wait for user input to continue. plt.cla() # Clear axes for next plot. However, when I run the script, all I see is an empty plot window,

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