Skip to content

Commit 566a1c3

Browse files
committed
TST: flk8
1 parent c38f70a commit 566a1c3

20 files changed

+81
-29
lines changed

lib/matplotlib/tests/test_constrainedlayout.py

+81-29
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,90 @@
88
from matplotlib.testing.decorators import image_comparison
99
import matplotlib.pyplot as plt
1010
import matplotlib.transforms as mtransforms
11+
import matplotlib.patches as mpatches
1112
from matplotlib import gridspec, ticker
1213

1314

15+
def fake_ylabel(ax, *, right=False, fontsize=12):
16+
"""
17+
Fake ylabel to test if the label is set correctly.
18+
"""
19+
fig = ax.figure
20+
width = fontsize / 72 * 1.2
21+
height = fontsize / 72 * 4
22+
trans = (fig.dpi_scale_trans +
23+
mtransforms.ScaledTranslation(0, 0.5, ax.transAxes) +
24+
mtransforms.ScaledTranslation(-2*width, -height / 2,
25+
ax.figure.dpi_scale_trans))
26+
rect = mpatches.Rectangle((0, 0), width, height, transform=trans,
27+
color='red', alpha=0.5, clip_on=False)
28+
ax.add_patch(rect)
29+
30+
31+
def fake_xlabel(ax, *, right=False, fontsize=12):
32+
"""
33+
Fake xlabel to test if the label is set correctly.
34+
"""
35+
fig = ax.figure
36+
height = fontsize / 72 * 1.2
37+
width = fontsize / 72 * 4
38+
trans = (fig.dpi_scale_trans +
39+
mtransforms.ScaledTranslation(0.5, 0, ax.transAxes) +
40+
mtransforms.ScaledTranslation(-width / 2, -2*height,
41+
ax.figure.dpi_scale_trans))
42+
rect = mpatches.Rectangle((0, 0), width, height, transform=trans,
43+
color='red', alpha=0.5, clip_on=False)
44+
ax.add_patch(rect)
45+
46+
47+
def fake_title(ax, *, right=False, fontsize=12):
48+
"""
49+
Fake xlabel to test if the label is set correctly.
50+
"""
51+
fig = ax.figure
52+
height = fontsize / 72 * 1.2
53+
width = fontsize / 72 * 4
54+
# Create rectangle
55+
trans = (fig.dpi_scale_trans +
56+
mtransforms.ScaledTranslation(0.5, 1.0, ax.transAxes) +
57+
mtransforms.ScaledTranslation(-width / 2, 0.2*height,
58+
ax.figure.dpi_scale_trans))
59+
60+
rect = mpatches.Rectangle((0, 0), width, height, transform=trans,
61+
color='blue', alpha=0.5, clip_on=False)
62+
ax.add_patch(rect)
63+
64+
1465
def example_plot(ax, fontsize=12, nodec=False):
1566
ax.plot([1, 2])
1667
ax.locator_params(nbins=3)
1768
if not nodec:
18-
ax.set_xlabel('x-label', fontsize=fontsize)
19-
ax.set_ylabel('y-label', fontsize=fontsize)
20-
ax.set_title('Title', fontsize=fontsize)
69+
fake_ylabel(ax, fontsize=fontsize)
70+
fake_xlabel(ax, fontsize=fontsize)
71+
fake_title(ax, fontsize=fontsize)
72+
# fake_xticks(ax, fontsize=10)
73+
ax.set_xticklabels([])
74+
ax.set_yticklabels([])
2175
else:
2276
ax.set_xticklabels([])
2377
ax.set_yticklabels([])
2478

2579

26-
def example_pcolor(ax, fontsize=12):
80+
def example_pcolor(ax, xlabel=True, ylabel=True, title=True, fontsize=12):
2781
dx, dy = 0.6, 0.6
2882
y, x = np.mgrid[slice(-3, 3 + dy, dy),
2983
slice(-3, 3 + dx, dx)]
3084
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
3185
pcm = ax.pcolormesh(x, y, z[:-1, :-1], cmap='RdBu_r', vmin=-1., vmax=1.,
3286
rasterized=True)
33-
ax.set_xlabel('x-label', fontsize=fontsize)
34-
ax.set_ylabel('y-label', fontsize=fontsize)
35-
ax.set_title('Title', fontsize=fontsize)
87+
if xlabel:
88+
fake_xlabel(ax, fontsize=fontsize)
89+
if ylabel:
90+
fake_ylabel(ax, fontsize=fontsize)
91+
if title:
92+
fake_title(ax, fontsize=fontsize)
93+
ax.set_xticklabels([])
94+
ax.set_yticklabels([])
3695
return pcm
3796

3897

@@ -52,7 +111,7 @@ def test_constrained_layout2():
52111
example_plot(ax, fontsize=24)
53112

54113

55-
@image_comparison(['constrained_layout3.png'])
114+
@image_comparison(['constrained_layout3.png'], remove_text=True)
56115
def test_constrained_layout3():
57116
"""Test constrained_layout for colorbars with subplots"""
58117

@@ -66,7 +125,7 @@ def test_constrained_layout3():
66125
fig.colorbar(pcm, ax=ax, pad=pad)
67126

68127

69-
@image_comparison(['constrained_layout4.png'])
128+
@image_comparison(['constrained_layout4.png'], remove_text=True)
70129
def test_constrained_layout4():
71130
"""Test constrained_layout for a single colorbar with subplots"""
72131

@@ -76,7 +135,7 @@ def test_constrained_layout4():
76135
fig.colorbar(pcm, ax=axs, pad=0.01, shrink=0.6)
77136

78137

79-
@image_comparison(['constrained_layout5.png'], tol=0.002)
138+
@image_comparison(['constrained_layout5.png'], tol=0.002, remove_text=True)
80139
def test_constrained_layout5():
81140
"""
82141
Test constrained_layout for a single colorbar with subplots,
@@ -91,7 +150,7 @@ def test_constrained_layout5():
91150
location='bottom')
92151

93152

94-
@image_comparison(['constrained_layout6.png'], tol=0.002)
153+
@image_comparison(['constrained_layout6.png'], tol=0.002, remove_text=True)
95154
def test_constrained_layout6():
96155
"""Test constrained_layout for nested gridspecs"""
97156
# Remove this line when this test image is regenerated.
@@ -106,7 +165,6 @@ def test_constrained_layout6():
106165
ax = fig.add_subplot(gs)
107166
axsl += [ax]
108167
example_plot(ax, fontsize=12)
109-
ax.set_xlabel('x-label\nMultiLine')
110168
axsr = []
111169
for gs in gsr:
112170
ax = fig.add_subplot(gs)
@@ -154,7 +212,7 @@ def test_constrained_layout7():
154212
fig.draw_without_rendering()
155213

156214

157-
@image_comparison(['constrained_layout8.png'])
215+
@image_comparison(['constrained_layout8.png'], remove_text=True)
158216
def test_constrained_layout8():
159217
"""Test for gridspecs that are not completely full"""
160218

@@ -189,9 +247,7 @@ def test_constrained_layout9():
189247
fig, axs = plt.subplots(2, 2, layout="constrained",
190248
sharex=False, sharey=False)
191249
for ax in axs.flat:
192-
pcm = example_pcolor(ax, fontsize=24)
193-
ax.set_xlabel('')
194-
ax.set_ylabel('')
250+
pcm = example_pcolor(ax, fontsize=24, xlabel=False, ylabel=False)
195251
ax.set_aspect(2.)
196252
fig.colorbar(pcm, ax=axs, pad=0.01, shrink=0.6)
197253
fig.suptitle('Test Suptitle', fontsize=28)
@@ -207,7 +263,7 @@ def test_constrained_layout10():
207263
ax.legend(loc='center left', bbox_to_anchor=(0.8, 0.5))
208264

209265

210-
@image_comparison(['constrained_layout11.png'])
266+
@image_comparison(['constrained_layout11.png'], remove_text=True)
211267
def test_constrained_layout11():
212268
"""Test for multiple nested gridspecs"""
213269

@@ -227,7 +283,7 @@ def test_constrained_layout11():
227283
example_plot(ax, fontsize=9)
228284

229285

230-
@image_comparison(['constrained_layout11rat.png'])
286+
@image_comparison(['constrained_layout11rat.png'], remove_text=True)
231287
def test_constrained_layout11rat():
232288
"""Test for multiple nested gridspecs with width_ratios"""
233289

@@ -266,10 +322,10 @@ def test_constrained_layout12():
266322
example_plot(ax, nodec=True)
267323
ax = fig.add_subplot(gs0[4:, 0])
268324
example_plot(ax, nodec=True)
269-
ax.set_xlabel('x-label')
325+
fake_xlabel(ax)
270326

271327

272-
@image_comparison(['constrained_layout13.png'], tol=2.e-2)
328+
@image_comparison(['constrained_layout13.png'], tol=2.e-2, remove_text=True)
273329
def test_constrained_layout13():
274330
"""Test that padding works."""
275331
fig, axs = plt.subplots(2, 2, layout="constrained")
@@ -281,7 +337,7 @@ def test_constrained_layout13():
281337
fig.get_layout_engine().set(w_pad=24./72., h_pad=24./72.)
282338

283339

284-
@image_comparison(['constrained_layout14.png'])
340+
@image_comparison(['constrained_layout14.png'], remove_text=True)
285341
def test_constrained_layout14():
286342
"""Test that padding works."""
287343
fig, axs = plt.subplots(2, 2, layout="constrained")
@@ -302,7 +358,7 @@ def test_constrained_layout15():
302358
example_plot(ax, fontsize=12)
303359

304360

305-
@image_comparison(['constrained_layout16.png'])
361+
@image_comparison(['constrained_layout16.png'], remove_text=True)
306362
def test_constrained_layout16():
307363
"""Test ax.set_position."""
308364
fig, ax = plt.subplots(layout="constrained")
@@ -414,9 +470,7 @@ def test_colorbar_location():
414470

415471
fig, axs = plt.subplots(4, 5, layout="constrained")
416472
for ax in axs.flat:
417-
pcm = example_pcolor(ax)
418-
ax.set_xlabel('')
419-
ax.set_ylabel('')
473+
pcm = example_pcolor(ax, xlabel=False, ylabel=False, title=False)
420474
fig.colorbar(pcm, ax=axs[:, 1], shrink=0.4)
421475
fig.colorbar(pcm, ax=axs[-1, :2], shrink=0.5, location='bottom')
422476
fig.colorbar(pcm, ax=axs[0, 2:], shrink=0.5, location='bottom', pad=0.05)
@@ -470,7 +524,7 @@ def test_colorbar_align():
470524
cbs[3].ax.get_position().y0)
471525

472526

473-
@image_comparison(['test_colorbars_no_overlapV.png'], style='mpl20')
527+
@image_comparison(['test_colorbars_no_overlapV.png'], style='mpl20', remove_text=True)
474528
def test_colorbars_no_overlapV():
475529
fig = plt.figure(figsize=(2, 4), layout="constrained")
476530
axs = fig.subplots(2, 1, sharex=True, sharey=True)
@@ -479,13 +533,11 @@ def test_colorbars_no_overlapV():
479533
ax.tick_params(axis='both', direction='in')
480534
im = ax.imshow([[1, 2], [3, 4]])
481535
fig.colorbar(im, ax=ax, orientation="vertical")
482-
fig.suptitle("foo")
483536

484537

485-
@image_comparison(['test_colorbars_no_overlapH.png'], style='mpl20')
538+
@image_comparison(['test_colorbars_no_overlapH.png'], style='mpl20', remove_text=True)
486539
def test_colorbars_no_overlapH():
487540
fig = plt.figure(figsize=(4, 2), layout="constrained")
488-
fig.suptitle("foo")
489541
axs = fig.subplots(1, 2, sharex=True, sharey=True)
490542
for ax in axs:
491543
ax.yaxis.set_major_formatter(ticker.NullFormatter())

0 commit comments

Comments
 (0)