|
| 1 | +""" |
| 2 | +=================================================== |
| 3 | +Advanced subplot placement with the gridspec module |
| 4 | +=================================================== |
| 5 | +
|
| 6 | +The ``subplots`` function is good for building a uniform grid |
| 7 | +of axes. The ``gridspec`` module provides more control, but is |
| 8 | +proportionally more complex to use. |
| 9 | +
|
| 10 | +The API documentation for the ``gridspec`` is |
| 11 | +`here <../../users/gridspec.html>`_. |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +import matplotlib.pyplot as plt |
| 17 | +import matplotlib.gridspec as gridspec |
| 18 | + |
| 19 | + |
| 20 | +SIZE = (5, 5) |
| 21 | + |
| 22 | + |
| 23 | +############################################################################ |
| 24 | +# A basic 4-by-4 grid using both ``subplots`` and ``gridspec``. |
| 25 | +# This serves as a simple comparison of the two for basic usage. |
| 26 | +# This is the ``subplots`` example: |
| 27 | + |
| 28 | +fig1, f1_axes = plt.subplots(ncols=2, nrows=2, figsize=SIZE) |
| 29 | +fig1.tight_layout() |
| 30 | + |
| 31 | +############################################################################ |
| 32 | +# For basic usage like this, ``gridspec`` is perhaps overly verbose: |
| 33
10000
| + |
| 34 | +fig2 = plt.figure(figsize=SIZE) |
| 35 | +spec2 = gridspec.GridSpec(ncols=2, nrows=2) |
| 36 | +f2ax1 = fig2.add_subplot(spec2[0, 0]) |
| 37 | +f2ax2 = fig2.add_subplot(spec2[0, 1]) |
| 38 | +f2ax3 = fig2.add_subplot(spec2[1, 0]) |
| 39 | +f2ax4 = fig2.add_subplot(spec2[1, 1]) |
| 40 | +fig2.tight_layout() |
| 41 | + |
| 42 | +############################################################################ |
| 43 | +# When you want to have plots of different sizes, however, ``gridspec`` |
| 44 | +# becomes indispensable and provides a couple of options. |
| 45 | +# This example shows off using the ``width_ratios`` and ``height_ratio`` |
| 46 | +# options. |
| 47 | +# These keyword arguments are lists of numbers. Note that absolute values |
| 48 | +# are meaningless, only their relative ratios matter. |
| 49 | +# That means that ``width_ratios=[2, 4, 8]`` is equivalent to |
| 50 | +# `width_ratios=[1, 2, 4]`` within equally wide figures. |
| 51 | +# We'll blindly create the axes within ``for`` loops since we won't need |
| 52 | +# them later. |
| 53 | + |
| 54 | +fig3 = plt.figure(figsize=SIZE) |
| 55 | +widths = [2, 3, 1.5] |
| 56 | +heights = [1, 3, 2] |
| 57 | +spec3 = gridspec.GridSpec(ncols=3, nrows=3, width_ratios=widths, |
| 58 | + height_ratios=heights) |
| 59 | +for row in range(3): |
| 60 | + for col in range(3): |
| 61 | + ax = fig3.add_subplot(spec3[row, col]) |
| 62 | + label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row]) |
| 63 | + ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center') |
| 64 | + |
| 65 | +fig3.tight_layout() |
| 66 | + |
| 67 | +############################################################################# |
| 68 | +# Another method to tweak the size of each subplot is to add a subplot to |
| 69 | +# multiple cells of the gridspec. |
| 70 | + |
| 71 | +fig4 = plt.figure(figsize=SIZE) |
| 72 | +spec4 = gridspec.GridSpec(ncols=3, nrows=3) |
| 73 | +anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction', |
| 74 | + va='center', ha='center') |
| 75 | + |
| 76 | +fig4.add_subplot(spec4[0, 0]).annotate('GridSpec[0, 0]', **anno_opts) |
| 77 | +fig4.add_subplot(spec4[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts) |
| 78 | +fig4.add_subplot(spec4[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts) |
| 79 | +fig4.add_subplot(spec4[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts) |
| 80 | + |
| 81 | +fig4.tight_layout() |
0 commit comments