Matplotlib also supports a great number of graphs used to depict more complex data. Here's a short list of some of the graphs available:

• Functions contour() and contourf() are used for contour plots. Contour plots draw a line connecting equal (x, y) value pairs. They're used in weather maps, detailing lines of equal pressure or temperature; in topographical maps, detailing the terrain; in physics graphs, to describe fields; and more.

• Function specgram() displays the frequency contents of data over time. specgram() can be used, for example, to plot the frequencies of a sound wave as a function of time.

• Boththe contour() and specgram() functions rely on a color map to depict the data. Color maps are a relation between a value and a color. Matplotlib provides a set of color maps that include such names as autumn() and hot() to ease the selection of a color map.

• Function quiver() implements quiver plots, which are typically used to describe force fields in physics. The quiver plot is a set of arrows depicting the force at each point (direction and magnitude).

Example: Plotting Frequency Content of a Signal

At times it's of value to plot the frequencies a signal is composed of as a function of time. For example, in an audio signal, a different frequency means a different note, so plotting frequencies as a function of time is a possible "musical visualization."

In this example, shown in Listing 6-13, we create a signal composed of several discrete frequencies and display those frequencies as a function of time using a specgram().

Listing 6-13. Specgram of a Signal from pylab import * Fs = 256

for t, f in zip(times, frequencies): x = cos(2*pi*arange(t*Fs)/Fs*f) y = append(y, x)

specgram(y, 256, Fs) xlabel('Time [sec]') ylabel('Frequency [Hz]')

I've set the frequency of sampling at 256 samples per second and created a signal composed of 100 Hertz (Hz) for 3 seconds, 20 Hz for 7 seconds, and then 80 Hz for 5 seconds. I then plot the signal using specgram(), with the results shown in Figure 6-15. Time [sec]

Figure 6-15. A specgram

Time [sec]

### Figure 6-15. A specgram

Figure 6-15 clearly shows that in the first 2 seconds the frequency is 100 Hz, in the next 8 seconds the frequency is 20 Hz, and in the last 5 seconds the signal's frequency is 80 Hz.

■ Note If you look closely at the figure, you'll notice there's a half-a-second shift in the specgram. This is due to an overlapping window of size 128 samples. See help(specgram) for information on the overlapping window.

You can change the colors used to display the specgram using a color map function. Simply issue hot() or autumn() at the end of the script, and observe the results. See help(colormaps) for a full account of available colormaps.

Example: A Repelling Force Field

The following example illustrates the use of quiver() to depict a force field. At each point in the figure, an arrow points at the direction of the acting force as well as its magnitude, denoted by the size of the arrow.

from pylab import *

title('A repelling force field!')

I've made use of the function meshgrid(x, y), which generates two matrices: the first is a matrix of repeating values of x, and the second is a matrix of repeating values of y. The output is used to plot the quiver, shown in Figure 6-16. I then update the axis to reflect the proper ranges.

 \ \ \ V \ t / t / / X \ \ \ * t t t / / V \ \ * t i t X X s*. "•v. X X y V i 4 * r Jr Jf Jr y > y > t f T K \ V X y / / / t t H V \ Hv V / / / I t \ V \ \ V / / / / 1 1 \ \ \ \ V

Figure 6-16. A quiver plot depicting a force field