Skip to content

Commit 9912534

Browse files
committed
TST: Make determinism test plots look less pathological
Currently, half the Axes are in a 2x2 grid, half are over the whole figure, and half are in a 1x6 grid (yes, that's more than 100% since they all overlap). Instead, place each optional section in a subfigure, and auto-size the figure based on how many sections there are.
1 parent bff64cc commit 9912534

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

lib/matplotlib/tests/test_determinism.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@ def _save_figure(objects='mhip', fmt="pdf", usetex=False):
2626
mpl.use(fmt)
2727
mpl.rcParams.update({'svg.hashsalt': 'asdf', 'text.usetex': usetex})
2828

29-
fig = plt.figure()
30-
31-
if 'm' in objects:
29+
def plot_markers(fig):
3230
# use different markers...
33-
ax1 = fig.add_subplot(1, 6, 1)
31+
ax = fig.add_subplot()
3432
x = range(10)
35-
ax1.plot(x, [1] * 10, marker='D')
36-
ax1.plot(x, [2] * 10, marker='x')
37-
ax1.plot(x, [3] * 10, marker='^')
38-
ax1.plot(x, [4] * 10, marker='H')
39-
ax1.plot(x, [5] * 10, marker='v')
33+
ax.plot(x, [1] * 10, marker='D')
34+
ax.plot(x, [2] * 10, marker='x')
35+
ax.plot(x, [3] * 10, marker='^')
36+
ax.plot(x, [4] * 10, marker='H')
37+
ax.plot(x, [5] * 10, marker='v')
4038

41-
if 'h' in objects:
39+
def plot_hatch(fig):
4240
# also use different hatch patterns
43-
ax2 = fig.add_subplot(1, 6, 2)
41+
ax2 = fig.add_subplot()
4442
bars = (ax2.bar(range(1, 5), range(1, 5)) +
4543
ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5)))
4644
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
@@ -49,17 +47,17 @@ def _save_figure(objects='mhip', fmt="pdf", usetex=False):
4947
for bar, pattern in zip(bars, patterns):
5048
bar.set_hatch(pattern)
5149

52-
if 'i' in objects:
50+
def plot_image(fig):
51+
axs = fig.subplots(1, 3, sharex=True, sharey=True)
5352
# also use different images
5453
A = [[1, 2, 3], [2, 3, 1], [3, 1, 2]]
55-
fig.add_subplot(1, 6, 3).imshow(A, interpolation='nearest')
54+
axs[0].imshow(A, interpolation='nearest')
5655
A = [[1, 3, 2], [1, 2, 3], [3, 1, 2]]
57-
fig.add_subplot(1, 6, 4).imshow(A, interpolation='bilinear')
56+
axs[1].imshow(A, interpolation='bilinear')
5857
A = [[2, 3, 1], [1, 2, 3], [2, 1, 3]]
59-
fig.add_subplot(1, 6, 5).imshow(A, interpolation='bicubic')
60-
61-
if 'p' in objects:
58+
axs[2].imshow(A, interpolation='bicubic')
6259

60+
def plot_paths(fig):
6361
# clipping support class, copied from demo_text_path.py gallery example
6462
class PathClippedImagePatch(PathPatch):
6563
"""
@@ -85,21 +83,23 @@ def draw(self, renderer=None):
8583
self.bbox_image.draw(renderer)
8684
super().draw(renderer)
8785

86+
subfigs = fig.subfigures(1, 3)
87+
8888
# add a polar projection
89-
px = fig.add_subplot(projection="polar")
89+
px = subfigs[0].add_subplot(projection="polar")
9090
pimg = px.imshow([[2]])
9191
pimg.set_clip_path(Circle((0, 1), radius=0.3333))
9292

9393
# add a text-based clipping path (origin: demo_text_path.py)
94-
(ax1, ax2) = fig.subplots(2)
94+
ax = subfigs[1].add_subplot()
9595
arr = plt.imread(get_sample_data("grace_hopper.jpg"))
9696
text_path = TextPath((0, 0), "!?", size=150)
9797
p = PathClippedImagePatch(text_path, arr, ec="k")
9898
offsetbox = AuxTransformBox(IdentityTransform())
9999
offsetbox.add_artist(p)
100100
ao = AnchoredOffsetbox(loc='upper left', child=offsetbox, frameon=True,
101101
borderpad=0.2)
102-
ax1.add_artist(ao)
102+
ax.add_artist(ao)
103103

104104
# add a 2x2 grid of path-clipped axes (origin: test_artist.py)
105105
exterior = Path.unit_rectangle().deepcopy()
@@ -112,7 +112,8 @@ def draw(self, renderer=None):
112112
star = Path.unit_regular_star(6).deepcopy()
113113
star.vertices *= 2.6
114114

115-
(row1, row2) = fig.subplots(2, 2, sharex=True, sharey=True)
115+
(row1, row2) = subfigs[2].subplots(2, 2, sharex=True, sharey=True,
116+
gridspec_kw=dict(hspace=0, wspace=0))
116117
for row in (row1, row2):
117118
ax1, ax2 = row
118119
collection = PathCollection([star], lw=5, edgecolor='blue',
@@ -128,8 +129,22 @@ def draw(self, renderer=None):
128129
ax1.set_xlim([-3, 3])
129130
ax1.set_ylim([-3, 3])
130131

132+
nfigs = len(objects) + 1
133+
fig = plt.figure(figsize=(7, 3 * nfigs))
134+
subfigs = iter(fig.subfigures(nfigs, squeeze=False).flat)
135+
fig.subplots_adjust(bottom=0.15)
136+
137+
if 'm' in objects:
138+
plot_markers(next(subfigs))
139+
if 'h' in objects:
140+
plot_hatch(next(subfigs))
141+
if 'i' in objects:
142+
plot_image(next(subfigs))
143+
if 'p' in objects:
144+
plot_paths(next(subfigs))
145+
131146
x = range(5)
132-
ax = fig.add_subplot(1, 6, 6)
147+
ax = next(subfigs).add_subplot()
133148
ax.plot(x, x)
134149
ax.set_title('A string $1+2+\\sigma$')
135150
ax.set_xlabel('A string $1+2+\\sigma$')
@@ -147,17 +162,15 @@ def draw(self, renderer=None):
147162
("i", "pdf", False),
148163
("mhip", "pdf", False),
149164
("mhip", "ps", False),
150-
pytest.param(
151-
"mhip", "ps", True, marks=[needs_usetex, needs_ghostscript]),
165+
pytest.param("mhip", "ps", True, marks=[needs_usetex, needs_ghostscript]),
152166
("p", "svg", False),
153167
("mhip", "svg", False),
154168
pytest.param("mhip", "svg", True, marks=needs_usetex),
155169
]
156170
)
157171
def test_determinism_check(objects, fmt, usetex):
158172
"""
159-
Output three times the same graphs and checks that the outputs are exactly
160-
the same.
173+
Output the same graph three times and check that the outputs are exactly the same.
161174
162175
Parameters
163176
----------
@@ -197,10 +210,11 @@ def test_determinism_check(objects, fmt, usetex):
197210
)
198211
def test_determinism_source_date_epoch(fmt, string):
199212
"""
200-
Test SOURCE_DATE_EPOCH support. Output a document with the environment
201-
variable SOURCE_DATE_EPOCH set to 2000-01-01 00:00 UTC and check that the
202-
document contains the timestamp that corresponds to this date (given as an
203-
argument).
213+
Test SOURCE_DATE_EPOCH support.
214+
215+
Output a document with the environment variable SOURCE_DATE_EPOCH set to
216+
2000-01-01 00:00 UTC and check that the document contains the timestamp that
217+
corresponds to this date (given as an argument).
204218
205219
Parameters
206220
----------

0 commit comments

Comments
 (0)