|
| 1 | +.. _gridspec-guide: |
| 2 | + |
| 3 | + |
| 4 | +************************************************ |
| 5 | + Customizing Location of Subplot Using GridSpec |
| 6 | +************************************************ |
| 7 | + |
| 8 | + ``GridSpec`` |
| 9 | + specifies the geometry of the grid that a subplot will be |
| 10 | + placed. The number of rows and number of columns of the grid |
| 11 | + need to be set. Optionally, the subplot layout parameters |
| 12 | + (e.g., left, right, etc.) can be tuned. |
| 13 | + |
| 14 | + ``SubplotSpec`` |
| 15 | + specifies the location of the subplot in the given *GridSpec*. |
| 16 | + |
| 17 | + ``subplot2grid`` |
| 18 | + a helper function that is similar to "pyplot.subplot" but uses |
| 19 | + 0-based indexing and let subplot to occupy multiple cells. |
| 20 | + |
| 21 | + |
| 22 | + Basic Example of using subplot2grid |
| 23 | +===================================== |
| 24 | + |
| 25 | +To use subplot2grid, you provide geometry of the grid and the location |
| 26 | +of the subplot in the grid. For a simple single-cell subplot, :: |
| 27 | + |
| 28 | + ax = plt.subplot2grid((2,2),(0, 0)) |
| 29 | + |
| 30 | +is identical to :: |
| 31 | + |
| 32 | + ax = plt.subplot(2,2,1) |
| 33 | + |
| 34 | +Note that, unlike matplotlib's subplot, the index starts from 0 in gridspec. |
| 35 | + |
| 36 | +To create a subplot that spans multiple cells, :: |
| 37 | + |
| 38 | + ax2 = plt.subplot2grid((3,3), (1, 0), colspan=2) |
| 39 | + ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2) |
| 40 | + |
| 41 | +For example, the following commands :: |
| 42 | + |
| 43 | + ax1 = plt.subplot2grid((3,3), (0,0), colspan=3) |
| 44 | + ax2 = plt.subplot2grid((3,3), (1,0), colspan=2) |
| 45 | + ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2) |
| 46 | + ax4 = plt.subplot2grid((3,3), (2, 0)) |
| 47 | + ax5 = plt.subplot2grid((3,3), (2, 1)) |
| 48 | + |
| 49 | +creates |
| 50 | + |
| 51 | +.. plot:: users/plotting/examples/demo_gridspec01.py |
| 52 | + |
| 53 | + |
| 54 | +GridSpec and SubplotSpec |
| 55 | +======================== |
| 56 | + |
| 57 | +You can create GridSpec explicitly and use them to create a Subplot. |
| 58 | + |
| 59 | +For example, :: |
| 60 | + |
| 61 | + ax = plt.subplot2grid((2,2),(0, 0)) |
| 62 | + |
| 63 | +is equal to :: |
| 64 | + |
| 65 | + import matplotlib.gridspec as gridspec |
| 66 | + gs = gridspec.GridSpec(2, 2) |
| 67 | + ax = plt.subplot(gs[0, 0]) |
| 68 | + |
| 69 | +A gridspec instance provides array-like (2d or 1d) indexing that |
| 70 | +returns the SubplotSpec instance. For, SubplotSpec that spans multiple |
| 71 | +cells, use slice. :: |
| 72 | + |
| 73 | + ax2 = plt.subplot(gs[1,:-1]) |
| 74 | + ax3 = plt.subplot(gs[1:, -1]) |
| 75 | + |
| 76 | +The above example becomes :: |
| 77 | + |
| 78 | + gs = gridspec.GridSpec(3, 3) |
| 79 | + ax1 = plt.subplot(gs[0, :]) |
| 80 | + ax2 = plt.subplot(gs[1,:-1]) |
| 81 | + ax3 = plt.subplot(gs[1:, -1]) |
| 82 | + ax4 = plt.subplot(gs[-1,0]) |
| 83 | + ax5 = plt.subplot(gs[-1,-2]) |
| 84 | + |
| 85 | +.. plot:: users/plotting/examples/demo_gridspec02.py |
| 86 | + |
| 87 | +Adjust GridSpec layout |
| 88 | +====================== |
| 89 | + |
| 90 | +When a GridSpec is explicitly used, you can adjust the layout |
| 91 | +parameters of subplots that are created from the gridspec. :: |
| 92 | + |
| 93 | + gs1 = gridspec.GridSpec(3, 3) |
| 94 | + gs1.update(left=0.05, right=0.48, wspace=0.05) |
| 95 | + |
| 96 | +This is similar to *subplots_adjust*, but it only affects the subplots |
| 97 | +that are created from the given GridSpec. |
| 98 | + |
| 99 | +The code below :: |
| 100 | + |
| 101 | + gs1 = gridspec.GridSpec(3, 3) |
| 102 | + gs1.update(left=0.05, right=0.48, wspace=0.05) |
| 103 | + ax1 = plt.subplot(gs1[:-1, :]) |
| 104 | + ax2 = plt.subplot(gs1[-1, :-1]) |
| 105 | + ax3 = plt.subplot(gs1[-1, -1]) |
| 106 | + |
| 107 | + gs2 = gridspec.GridSpec(3, 3) |
| 108 | + gs2.update(left=0.55, right=0.98, hspace=0.05) |
| 109 | + ax4 = plt.subplot(gs2[:, :-1]) |
| 110 | + ax5 = plt.subplot(gs2[:-1, -1]) |
| 111 | + ax6 = plt.subplot(gs2[-1, -1]) |
| 112 | + |
| 113 | +creates |
| 114 | + |
| 115 | +.. plot:: users/plotting/examples/demo_gridspec03.py |
| 116 | + |
| 117 | +GridSpec using SubplotSpec |
| 118 | +========================== |
| 119 | + |
| 120 | +You can create GridSpec from the SubplotSpec, in which case its layout |
| 121 | +parameters are set to that of the locataion of the given SubplotSpec. :: |
| 122 | + |
| 123 | + gs0 = gridspec.GridSpec(1, 2) |
| 124 | + |
| 125 | + gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0]) |
| 126 | + gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1]) |
| 127 | + |
| 128 | + |
| 129 | +.. plot:: users/plotting/examples/demo_gridspec04.py |
| 130 | + |
0 commit comments