Skip to content

Commit a85440e

Browse files
committed
TST + DOC
1 parent 6fa3fa9 commit a85440e

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

lib/matplotlib/tests/test_constrainedlayout.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ def example_plot(ax, fontsize=12, nodec=False):
2020
ax.set_yticklabels('')
2121

2222

23-
def example_pcolor(ax, fontsize=12):
23+
def example_pcolor(ax, fontsize=12, hide_labels=False):
2424
dx, dy = 0.6, 0.6
2525
y, x = np.mgrid[slice(-3, 3 + dy, dy),
2626
slice(-3, 3 + dx, dx)]
2727
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
2828
pcm = ax.pcolormesh(x, y, z[:-1, :-1], cmap='RdBu_r', vmin=-1., vmax=1.,
2929
rasterized=True)
30-
ax.set_xlabel('x-label', fontsize=fontsize)
31-
ax.set_ylabel('y-label', fontsize=fontsize)
32-
ax.set_title('Title', fontsize=fontsize)
30+
if not hide_labels:
31+
ax.set_xlabel('x-label', fontsize=fontsize)
32+
ax.set_ylabel('y-label', fontsize=fontsize)
33+
ax.set_title('Title', fontsize=fontsize)
3334
return pcm
3435

3536

@@ -555,3 +556,34 @@ def test_align_labels():
555556
after_align[1].x0, rtol=0, atol=1e-05)
556557
# ensure labels do not go off the edge
557558
assert after_align[0].x0 >= 1
559+
560+
561+
def test_compressed1():
562+
fig, axs = plt.subplots(3, 2, constrained_layout={'compress': True},
563+
sharex=True, sharey=True)
564+
for ax in axs.flat:
565+
pc = ax.imshow(np.random.randn(20, 20))
566+
567+
fig.colorbar(pc, ax=axs)
568+
fig.draw_no_output()
569+
570+
pos = axs[0, 0].get_position()
571+
np.testing.assert_allclose(pos.x0, 0.2244, atol=1e-3)
572+
pos = axs[0, 1].get_position()
573+
np.testing.assert_allclose(pos.x1, 0.6925, atol=1e-3)
574+
575+
# wider than tall
576+
fig, axs = plt.subplots(2, 3, constrained_layout={'compress': True},
577+
sharex=True, sharey=True, figsize=(5, 4))
578+
for ax in axs.flat:
579+
pc = ax.imshow(np.random.randn(20, 20))
580+
581+
fig.colorbar(pc, ax=axs)
582+
fig.draw_no_output()
583+
584+
pos = axs[0, 0].get_position()
585+
np.testing.assert_allclose(pos.x0, 0.06195, atol=1e-3)
586+
np.testing.assert_allclose(pos.y1, 0.8413, atol=1e-3)
587+
pos = axs[1, 2].get_position()
588+
np.testing.assert_allclose(pos.x1, 0.832587, atol=1e-3)
589+
np.testing.assert_allclose(pos.y0, 0.205377, atol=1e-3)

tutorials/intermediate/constrainedlayout_guide.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import matplotlib.pyplot as plt
5353
import matplotlib.colors as mcolors
5454
import matplotlib.gridspec as gridspec
55+
from matplotlib.tests.test_constrainedlayout import example_pcolor
5556
import numpy as np
5657

5758
plt.rcParams['savefig.facecolor'] = "0.8"
@@ -230,8 +231,67 @@ def example_plot(ax, fontsize=12, hide_labels=False):
230231
# :align: center
231232
#
232233

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+
233293
###############################################################################
234-
# Padding and Spacing
294+
# Padding and spacing
235295
# ===================
236296
#
237297
# Padding between axes is controlled in the horizontal by *w_pad* and

0 commit comments

Comments
 (0)