## Subplots

In some of the previous figures in this chapter, I've displayed several smaller graphs in one figure; these are known as subplots. The subplot() function splits the figure into subplots and selects the current subplot. The subplots are numbered from left to right, top to bottom, so the upper-left subplot is 1, and the lower-right subplot is equivalent to the number of subplots. Notice that this is different from the default counting behavior used in Python: numbers start at 1 and not at 0.

To split the figure into 2-by-2 subplots and select the upper-left subplot for plotting, issue subplot(2, 2, l). Alternatively, you can pass the string '221', which does the same thing: subplot('221'). It's also possible to combine subplots of different sizes in one figure. This is a bit tricky and requires subplotting with different subplot sizes. Listing 6-5 gives an example that generates a subplot on the upper half of the figure and two subplots on the lower part of the figure, the results of which you can see in Figure 6-6.

Listing 6-5. Subplots of Varying Sizes figure()

subplot(2, 1, l) title('Upper half') subplot(2, 2, 3) title('Lower half, left side') subplot(2, 2, 4) title('Lower half, right side')

Figure 6-5 shows the output generated from Listing 6-4 without issuing the last two calls to xticks() and yticks() (left graph) and with xticks() and yticks() calls (right graph). Notice the labels on the x-axis.

Figure 6-6. Subplots of varying sizes

Figure 6-6. Subplots of varying sizes

■rip Subplots are especially useful in visualizing several aspects of the same data. For example, the GPS example in Chapter 1 shows x and y coordinates in one subplot and velocity in another subplot. Events (e.g., speeding) are marked in both, providing a visual link between the two subplots.

Was this article helpful?

0 0

### Responses

• Carli
How to remove a subplot in python?
8 years ago