Plotting Graphs

■ Note Going forward, I'll omit the show() call from listings. Be sure to issue show() or savefig() if you'd like to follow along.

I've passed the vector y as an input to plot(). As a result, plot() drew a graph of the vector y using auto-incrementing integers for an x-axis. Which is to say that if you don't supply x-axis values, plot() will automatically generate one for you: plot(y) is equivalent to plot(range(len(y)), y). So let's supply x-axis values (denoted by variable t):

»> t = array([lO, 11, 12, 13]) >>> plot(t, y)

The call to function figure() generates a new figure to plot on so we don't overwrite the previous figure.

On to more options. Next we want to plot y as a function of t but display only markers, not lines. This is easily done:

To select a different marker, replace the character 'o' with any of the markers in Table 6-2. For a full account of available markers, issue help(plot).

Table 6-2. Some Plot Markers

Character Marker Symbol

'o' Circle

'A' Upward-pointing triangle

's' Square

'x' Cross (multiplication)

'D' Diamond

Much like there are different markers, there are also different line styles, a few of which are listed in Table 6-3.

Table 6-3. Some Plot Line Styles Character(s) Line Style

If you'd like both markers and lines, concatenate the symbols for line styles and markers. To plot a dash-dot line and diamond symbols as markers, issue the following:

Figure 6-2 shows the output of the examples in this section.

Figure 6-2. Output of previous examples. Order is from left to right, top to bottom.

Was this article helpful?

0 0

Post a comment