|
1 | 1 | """
|
2 |
| -============= |
3 |
| -Subplots Demo |
4 |
| -============= |
5 |
| -
|
6 |
| -Examples illustrating the use of plt.subplots(). |
7 |
| -
|
8 |
| -This function creates a figure and a grid of subplots with a single call, while |
9 |
| -providing reasonable control over how the individual plots are created. For |
10 |
| -very refined tuning of subplot creation, you can still use add_subplot() |
11 |
| -directly on a new figure. |
| 2 | +================================================ |
| 3 | +Creating multiple subplots using ``plt.subplot`` |
| 4 | +================================================ |
| 5 | +
|
| 6 | +`.pyplot.subplots` creates a figure and a grid of subplots with a single call, |
| 7 | +while providing reasonable control over how the individual plots are created. |
| 8 | +For more advanced use cases you can use `.GridSpec` for a more general subplot |
| 9 | +layout or `.Figure.add_subplot` for adding subplots at arbitrary locations |
| 10 | +within the figure. |
12 | 11 | """
|
13 | 12 |
|
| 13 | +# sphinx_gallery_thumbnail_number = 11 |
| 14 | + |
14 | 15 | import matplotlib.pyplot as plt
|
15 | 16 | import numpy as np
|
16 | 17 |
|
17 |
| -# Simple data to display in various forms |
| 18 | +# Some example data to display |
18 | 19 | x = np.linspace(0, 2 * np.pi, 400)
|
19 | 20 | y = np.sin(x ** 2)
|
20 | 21 |
|
21 |
| -plt.close('all') |
| 22 | +############################################################################### |
| 23 | +# A figure with just one subplot |
| 24 | +# """""""""""""""""""""""""""""" |
| 25 | +# |
| 26 | +# ``subplots()`` without arguments returns a `.Figure` and a single |
| 27 | +# `~.axes.Axes`. |
| 28 | +# |
| 29 | +# This is actually the simplest and recommended way of creating a single |
| 30 | +# Figure and Axes. |
| 31 | + |
| 32 | +fig, ax = plt.subplots() |
| 33 | +ax.plot(x, y) |
| 34 | +ax.set_title('A single plot') |
22 | 35 |
|
23 | 36 | ###############################################################################
|
24 |
| -# Just a figure and one subplot |
| 37 | +# Stacking subplots in one direction |
| 38 | +# """""""""""""""""""""""""""""""""" |
| 39 | +# |
| 40 | +# The first two optional arguments of `.pyplot.subplots` define the number of |
| 41 | +# rows and columns of the subplot grid. |
| 42 | +# |
| 43 | +# When stacking in one direction only, the returned `axs` is a 1D numpy array |
| 44 | +# containing the list of created Axes. |
| 45 | + |
| 46 | +fig, axs = plt.subplots(2) |
| 47 | +fig.suptitle('Vertically stacked subplots') |
| 48 | +axs[0].plot(x, y) |
| 49 | +axs[1].plot(x, -y) |
25 | 50 |
|
26 |
| -f, ax = plt.subplots() |
27 |
| -ax.plot(x, y) |
28 |
| -ax.set_title('Simple plot') |
| 51 | +############################################################################### |
| 52 | +# If you are creating just a few Axes, it's handy to unpack them immediately to |
| 53 | +# dedicated variables for each Axes. That way, we can use ``ax1`` instead of |
| 54 | +# the more verbose ``axs[0]``. |
| 55 | + |
| 56 | +fig, (ax1, ax2) = plt.subplots(2) |
| 57 | +fig.suptitle('Vertically stacked subplots') |
| 58 | +ax1.plot(x, y) |
| 59 | +ax2.plot(x, -y) |
29 | 60 |
|
30 | 61 | ###############################################################################
|
31 |
| -# Two subplots, the axes array is 1-d |
| 62 | +# To obtain side-by-side subplots, pass parameters ``1, 2`` for one row and two |
| 63 | +# columns. |
32 | 64 |
|
33 |
| -f, axarr = plt.subplots(2, sharex=True) |
34 |
| -f.suptitle('Sharing X axis') |
35 |
| -axarr[0].plot(x, y) |
36 |
| -axarr[1].scatter(x, y) |
| 65 | +fig, (ax1, ax2) = plt.subplots(1, 2) |
| 66 | +fig.suptitle('Horizontally stacked subplots') |
| 67 | +ax1.plot(x, y) |
| 68 | +ax2.plot(x, -y) |
| 69 | + |
| 70 | +############################################################################### |
| 71 | +# Stacking subplots in two directions |
| 72 | +# """"""""""""""""""""""""""""""""""" |
| 73 | +# |
| 74 | +# When stacking in two directions, the returned `axs` is a 2D numpy array. |
| 75 | +# |
| 76 | +# If you have to set parameters for each subplot it's handy to iterate over |
| 77 | +# all subplots in a 2D grid using ``for ax in axs.flat:``. |
| 78 | + |
| 79 | +fig, axs = plt.subplots(2, 2) |
| 80 | +axs[0, 0].plot(x, y) |
| 81 | +axs[0, 0].set_title('Axis [0,0]') |
| 82 | +axs[0, 1].plot(x, y, 'tab:orange') |
| 83 | +axs[0, 1].set_title('Axis [0,1]') |
| 84 | +axs[1, 0].plot(x, -y, 'tab:green') |
| 85 | +axs[1, 0].set_title('Axis [1,0]') |
| 86 | +axs[1, 1].plot(x, -y, 'tab:red') |
| 87 | +axs[1, 1].set_title('Axis [1,1]') |
| 88 | + |
| 89 | +for ax in axs.flat: |
| 90 | + ax.set(xlabel='x-label', ylabel='y-label') |
| 91 | + |
| 92 | +# Hide x labels and tick labels for top plots and y ticks for right plots. |
| 93 | +for ax in axs.flat: |
| 94 | + ax.label_outer() |
37 | 95 |
|
38 | 96 | ###############################################################################
|
39 |
| -# Two subplots, unpack the axes array immediately |
| 97 | +# You can use tuple-unpacking also in 2D to assign all subplots to dedicated |
| 98 | +# variables: |
40 | 99 |
|
41 |
| -f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) |
42 |
| -f.suptitle('Sharing Y axis') |
| 100 | +fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) |
| 101 | +fig.suptitle('Sharing x per column, y per row') |
43 | 102 | ax1.plot(x, y)
|
44 |
| -ax2.scatter(x, y) |
| 103 | +ax2.plot(x, y**2, 'tab:orange') |
| 104 | +ax3.plot(x, -y, 'tab:green') |
| 105 | +ax4.plot(x, -y**2, 'tab:red') |
| 106 | + |
| 107 | +for ax in axs.flat: |
| 108 | + ax.label_outer() |
| 109 | + |
| 110 | +############################################################################### |
| 111 | +# Sharing axes |
| 112 | +# """""""""""" |
| 113 | +# |
| 114 | +# By default, each Axes is scaled individually. Thus, if the ranges are |
| 115 | +# different the tick values of the subplots do not align. |
| 116 | + |
| 117 | +fig, (ax1, ax2) = plt.subplots(2) |
| 118 | +fig.suptitle('Axes values are scaled individually by default') |
| 119 | +ax1.plot(x, y) |
| 120 | +ax2.plot(x + 1, -y) |
| 121 | + |
| 122 | +############################################################################### |
| 123 | +# You can use *sharex* or *sharey* to align the horizontal or vertical axis. |
| 124 | + |
| 125 | +fig, (ax1, ax2) = plt.subplots(2, sharex=True) |
| 126 | +fig.suptitle('Aligning x-axis using sharex') |
| 127 | +ax1.plot(x, y) |
| 128 | +ax2.plot(x + 1, -y) |
45 | 129 |
|
46 | 130 | ###############################################################################
|
47 |
| -# Three subplots sharing both x/y axes |
48 |
| - |
49 |
| -f, axarr = plt.subplots(3, sharex=True, sharey=True) |
50 |
| -f.suptitle('Sharing both axes') |
51 |
| -axarr[0].plot(x, y) |
52 |
| -axarr[1].scatter(x, y) |
53 |
| -axarr[2].scatter(x, 2 * y ** 2 - 1, color='r') |
54 |
| -# Bring subplots close to each other. |
55 |
| -f.subplots_adjust(hspace=0) |
| 131 | +# Setting *sharex* or *sharey* to ``True`` enables global sharing across the |
| 132 | +# whole grid, i.e. also the y-axes of vertically stacked subplots have the |
| 133 | +# same scale when using ``sharey=True``. |
| 134 | + |
| 135 | +fig, axs = plt.subplots(3, sharex=True, sharey=True) |
| 136 | +fig.suptitle('Sharing both axes') |
| 137 | +axs[0].plot(x, y ** 2) |
| 138 | +axs[1].plot(x, 0.3 * y, 'o') |
| 139 | +axs[2].plot(x, y, '+') |
| 140 | + |
| 141 | +############################################################################### |
| 142 | +# For subplots that are sharing axes one set of tick labels is enough. Tick |
| 143 | +# labels of inner Axes are automatically removed by *sharex* and *sharey*. |
| 144 | +# Still there remains an unused empty space between the subplots. |
| 145 | +# |
| 146 | +# The parameter *gridspec_kw* of `.pyplot.subplots` controls the grid |
| 147 | +# properties (see also `.GridSpec`). For example, we can reduce the height |
| 148 | +# between vertical subplots using ``gridspec_kw={'hspace': 0}``. |
| 149 | +# |
| 150 | +# `.label_outer` is a handy method to remove labels and ticks from subplots |
| 151 | +# that are not at the edge of the grid. |
| 152 | + |
| 153 | +fig, axs = plt.subplots(3, sharex=True, sharey=True, gridspec_kw={'hspace': 0}) |
| 154 | +fig.suptitle('Sharing both axes') |
| 155 | +axs[0].plot(x, y ** 2) |
| 156 | +axs[1].plot(x, 0.3 * y, 'o') |
| 157 | +axs[2].plot(x, y, '+') |
| 158 | + |
56 | 159 | # Hide x labels and tick labels for all but bottom plot.
|
57 |
| -for ax in axarr: |
| 160 | +for ax in axs: |
58 | 161 | ax.label_outer()
|
59 | 162 |
|
60 | 163 | ###############################################################################
|
61 |
| -# Row and column sharing |
| 164 | +# Apart from ``True`` and ``False``, both *sharex* and *sharey* accept the |
| 165 | +# values 'row' and 'col' to share the values only per row or column. |
62 | 166 |
|
63 |
| -f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row') |
64 |
| -f.suptitle('Sharing x per column, y per row') |
| 167 | +fig, axs = plt.subplots(2, 2, sharex='col', sharey='row', |
| 168 | + gridspec_kw={'hspace': 0, 'wspace': 0}) |
| 169 | +(ax1, ax2), (ax3, ax4) = axs |
| 170 | +fig.suptitle('Sharing x per column, y per row') |
65 | 171 | ax1.plot(x, y)
|
66 |
| -ax2.scatter(x, y) |
67 |
| -ax3.scatter(x, 2 * y ** 2 - 1, color='r') |
68 |
| -ax4.plot(x, 2 * y ** 2 - 1, color='r') |
| 172 | +ax2.plot(x, y**2, 'tab:orange') |
| 173 | +ax3.plot(x + 1, -y, 'tab:green') |
| 174 | +ax4.plot(x + 2, -y**2, 'tab:red') |
69 | 175 |
|
70 |
| -############################################################################### |
71 |
| -# Four axes, returned as a 2-d array |
72 |
| - |
73 |
| -f, axarr = plt.subplots(2, 2) |
74 |
| -axarr[0, 0].plot(x, y) |
75 |
| -axarr[0, 0].set_title('Axis [0,0]') |
76 |
| -axarr[0, 1].scatter(x, y) |
77 |
| -axarr[0, 1].set_title('Axis [0,1]') |
78 |
| -axarr[1, 0].plot(x, y ** 2) |
79 |
| -axarr[1, 0].set_title('Axis [1,0]') |
80 |
| -axarr[1, 1].scatter(x, y ** 2) |
81 |
| -axarr[1, 1].set_title('Axis [1,1]') |
82 |
| -for ax in axarr.flat: |
83 |
| - ax.set(xlabel='x-label', ylabel='y-label') |
84 |
| -# Hide x labels and tick labels for top plots and y ticks for right plots. |
85 |
| -for ax in axarr.flat: |
| 176 | +for ax in axs.flat: |
86 | 177 | ax.label_outer()
|
87 | 178 |
|
88 | 179 | ###############################################################################
|
89 |
| -# Four polar axes |
90 |
| - |
91 |
| -f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar')) |
92 |
| -axarr[0, 0].plot(x, y) |
93 |
| -axarr[0, 0].set_title('Axis [0,0]') |
94 |
| -axarr[0, 1].scatter(x, y) |
95 |
| -axarr[0, 1].set_title('Axis [0,1]') |
96 |
| -axarr[1, 0].plot(x, y ** 2) |
97 |
| -axarr[1, 0].set_title('Axis [1,0]') |
98 |
| -axarr[1, 1].scatter(x, y ** 2) |
99 |
| -axarr[1, 1].set_title('Axis [1,1]') |
100 |
| -# Fine-tune figure; make subplots farther from each other. |
101 |
| -f.subplots_adjust(hspace=0.3) |
102 |
| - |
103 |
| -plt.show() |
| 180 | +# Polar axes |
| 181 | +# """""""""" |
| 182 | +# |
| 183 | +# The parameter *subplot_kw* of `.pyplot.subplots` controls the subplot |
| 184 | +# properties (see also `.Figure.add_subplot`). In particular, this can be used |
| 185 | +# to create a grid of polar Axes. |
| 186 | + |
| 187 | +fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar')) |
| 188 | +ax1.plot(x, y) |
| 189 | +ax2.plot(x, y ** 2) |
0 commit comments