|
52 | 52 | import matplotlib.pyplot as plt
|
53 | 53 | import matplotlib.colors as mcolors
|
54 | 54 | import matplotlib.gridspec as gridspec
|
| 55 | +from matplotlib.tests.test_constrainedlayout import example_pcolor |
55 | 56 | import numpy as np
|
56 | 57 |
|
57 | 58 | plt.rcParams['savefig.facecolor'] = "0.8"
|
@@ -230,8 +231,67 @@ def example_plot(ax, fontsize=12, hide_labels=False):
|
230 | 231 | # :align: center
|
231 | 232 | #
|
232 | 233 |
|
| 234 | +############################################################################## |
| 235 | +# Grids of fixed-aspect axes |
| 236 | +# ========================== |
| 237 | +# |
| 238 | +# Often we want to layout axes with fixed-aspect ratios. This adds an extra |
| 239 | +# constraint to the layout problem, which by default is solved by leaving |
| 240 | +# one dimension with large white space between axes: |
| 241 | + |
| 242 | +fig, axs = plt.subplots(2, 2, constrained_layout=True, figsize=(6, 3)) |
| 243 | +for ax in axs.flat: |
| 244 | + pc = example_pcolor(ax, hide_labels=True) |
| 245 | + ax.set_aspect(1) |
| 246 | +fig.colorbar(pc, ax=axs) |
| 247 | +fig.suptitle('Fixed-aspect axes') |
| 248 | + |
| 249 | +################################## |
| 250 | +# Now, we could change the size of the figure manually to improve the |
| 251 | +# whitespace, but that requires manual intervention. |
| 252 | +# To address this, we can set ``constrained_layout`` to "compress" the |
| 253 | +# axes: |
| 254 | +fig, axs = plt.subplots(2, 2, constrained_layout={'compress': True}, |
| 255 | + figsize=(6, 3), sharex=True, sharey=True) |
| 256 | +for ax in axs.flat: |
| 257 | + pc = example_pcolor(ax, hide_labels=True) |
| 258 | + ax.set_aspect(1) |
| 259 | +fig.colorbar(pc, ax=axs) |
| 260 | +fig.suptitle('Fixed-aspect axes') |
| 261 | + |
| 262 | +################################### |
| 263 | +# Note this works in the vertical direction as well, though the |
| 264 | +# suptitle stays at the top of the plot: |
| 265 | +fig, axs = plt.subplots(2, 2, constrained_layout={'compress': True}, |
| 266 | + figsize=(3, 5), sharex=True, sharey=True) |
| 267 | +for ax in axs.flat: |
| 268 | + pc = example_pcolor(ax, hide_labels=True) |
| 269 | + ax.set_aspect(1) |
| 270 | +fig.colorbar(pc, ax=axs) |
| 271 | +fig.suptitle('Fixed-aspect axes') |
| 272 | + |
| 273 | +################################### |
| 274 | +# Note if only one row of axes have a fixed aspect, there can still be |
| 275 | +# the need for manually adjusting the figure size, however, in this case |
| 276 | +# widening the figure will make the layout look good again (not shown here) |
| 277 | + |
| 278 | +fig, axs = plt.subplots(2, 2, constrained_layout={'compress': True}, |
| 279 | + figsize=(4, 6), sharex=True, sharey=True) |
| 280 | +for i in range(2): |
| 281 | + for j in range(2): |
| 282 | + ax = axs[i, j] |
| 283 | + pc = example_pcolor(ax, hide_labels=True) |
| 284 | + if i == 0: |
| 285 | + ax.set_title('asp=1') |
| 286 | + ax.set_aspect(1) |
| 287 | + else: |
| 288 | + ax.set_title('asp="auto"') |
| 289 | +fig.colorbar(pc, ax=axs) |
| 290 | +fig.suptitle('Fixed-aspect axes') |
| 291 | +plt.show() |
| 292 | + |
233 | 293 | ###############################################################################
|
234 |
| -# Padding and Spacing |
| 294 | +# Padding and spacing |
235 | 295 | # ===================
|
236 | 296 | #
|
237 | 297 | # Padding between axes is controlled in the horizontal by *w_pad* and
|
|
0 commit comments