Multiple plots in a single chart

It is often useful to contrast data by displaying multiple plots next to each other. This is actually quite easy to when using matplotlib.

To draw multiple subplots on a grid, we can make multiple calls to plt.subplot2grid(), each time passing the size of the grid the subplot is to be located on (shape=(height, width)) and the location on the grid of the upper-left section of the subplot (loc=(row, column)). Each call to plt.subplot2grid() returns a different AxesSubplot object that can be used to reference the specific subplot and direct the rendering into.

The following demonstrates this, by creating a plot with two subplots based on a two row by one column grid (shape=(2,1)). The first subplot, referred to by ax1, is located in the first row (loc=(0,0)), and the second, referred to as ax2, is in the second row (loc=(1,0)):

In [41]:
   # create two sub plots on the new plot using a 2x1 grid
   # ax1 is the upper row
   ax1 = plt.subplot2grid(shape=(2,1), loc=(0,0))
   # and ax2 is in the lower row
   ax2 = plt.subplot2grid(shape=(2,1), loc=(1,0))
Multiple plots in a single chart

The subplots have been created, but we have not drawn into either yet.

The size of any subplot can be specified using the rowspan and colspan parameters in each call to plt.subplot2grid(). This actually feels a lot like placing content in HTML tables.

The following demonstrates a more complicated layout of five plots, specifying different row and column spans for each:

In [42]:
   # layout sub plots on a 4x4 grid
   # ax1 on top row, 4 columns wide
   ax1 = plt.subplot2grid((4,4), (0,0), colspan=4)
   # ax2 is row 2, leftmost and 2 columns wide
   ax2 = plt.subplot2grid((4,4), (1,0), colspan=2)
   # ax3 is 2 cols wide and 2 rows high, starting
   # on second row and the third column
   ax3 = plt.subplot2grid((4,4), (1,2), colspan=2, rowspan=2)
   # ax4 1 high 1 wide, in row 4 column 0
   ax4 = plt.subplot2grid((4,4), (2,0))
   # ax4 1 high 1 wide, in row 4 column 1
   ax5 = plt.subplot2grid((4,4), (2,1));
Multiple plots in a single chart

To draw into a specific subplot using the pandas .plot() method, you can pass the specific axes into the plot function via the ax parameter. The following demonstrates this by extracting each series from the random walk we created at the beginning of the chapter, and drawing each into different subplots:

In [43]:
   # demonstrating drawing into specific sub-plots
   # generate a layout of 2 rows 1 column
   # create the subplots, one on each row
   ax5 = plt.subplot2grid((2,1), (0,0))
   ax6 = plt.subplot2grid((2,1), (1,0))
   # plot column 0 of walk_df into top row of the grid
   walk_df[[0]].plot(ax = ax5)
   # and column 1 of walk_df into bottom row
   walk_df[[1]].plot(ax = ax6);
Multiple plots in a single chart

Using this technique, we can perform combinations of different series of data, such as a stock close versus volume graph. Given the data we read during a previous example for Google, the following will plot the volume versus the closing price:

In [44]:
   # draw the close on the top chart
   top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4)
   top.plot(stock_data.index, stock_data['Close'], label='Close')
   plt.title('Google Opening Stock Price 2001')

   # draw the volume chart on the bottom
   bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
   bottom.bar(stock_data.index, stock_data['Volume'])
   plt.title('Google Trading Volume')

   # set the size of the plot
   plt.gcf().set_size_inches(15,8)
Multiple plots in a single chart
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.197.251