Skip to content

Commit 850cad7

Browse files
committed
TST: Use text placeholders for empty legends
These tests use `remove_text=True` and set legend labels to empty strings. However, they are still affected by font metrics because even the empty string is as tall as the line height (which is calculated from the height of the "lp" string.)
1 parent f101e49 commit 850cad7

File tree

12 files changed

+532
-388
lines changed

12 files changed

+532
-388
lines changed
Loading
Binary file not shown.
Binary file not shown.
Loading

lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight.svg

+513-366
Loading
Loading
Loading

lib/matplotlib/tests/test_axes.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -4593,14 +4593,12 @@ def test_hist_stacked_weighted():
45934593

45944594

45954595
@image_comparison(['stem.png'], style='mpl20', remove_text=True)
4596-
def test_stem():
4596+
def test_stem(text_placeholders):
45974597
x = np.linspace(0.1, 2 * np.pi, 100)
45984598

45994599
fig, ax = plt.subplots()
4600-
# Label is a single space to force a legend to be drawn, but to avoid any
4601-
# text being drawn
46024600
ax.stem(x, np.cos(x),
4603-
linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label=' ')
4601+
linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label='stem')
46044602
ax.legend()
46054603

46064604

lib/matplotlib/tests/test_backend_pdf.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ def test_pdfpages_fspath():
296296
pdf.savefig(plt.figure())
297297

298298

299-
@image_comparison(['hatching_legend.pdf'])
300-
def test_hatching_legend():
299+
@image_comparison(['hatching_legend.pdf'], style='mpl20')
300+
def test_hatching_legend(text_placeholders):
301301
"""Test for correct hatching on patches in legend"""
302-
fig = plt.figure(figsize=(1, 2))
302+
fig = plt.figure(figsize=(1.5, 1))
303303

304304
a = Rectangle([0, 0], 0, 0, facecolor="green", hatch="XXXX")
305305
b = Rectangle([0, 0], 0, 0, facecolor="blue", hatch="XXXX")
306306

307-
fig.legend([a, b, a, b], ["", "", "", ""])
307+
fig.legend([a, b, a, b], ["green", "blue", "green", "blue"])
308308

309309

310310
@image_comparison(['grayscale_alpha.pdf'])

lib/matplotlib/tests/test_bbox_tight.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
1212

1313

14-
@image_comparison(['bbox_inches_tight'], remove_text=True,
14+
@image_comparison(['bbox_inches_tight'], remove_text=True, style='mpl20',
1515
savefig_kwarg={'bbox_inches': 'tight'})
16-
def test_bbox_inches_tight():
16+
def test_bbox_inches_tight(text_placeholders):
1717
#: Test that a figure saved using bbox_inches='tight' is clipped correctly
1818
data = [[66386, 174296, 75131, 577908, 32015],
1919
[58230, 381139, 78045, 99308, 160454],
2020
[89135, 80552, 152558, 497981, 603535],
2121
[78415, 81858, 150656, 193263, 69638],
2222
[139361, 331509, 343164, 781380, 52269]]
2323

24-
col_labels = row_labels = [''] * 5
24+
col_labels = row_labels = ['a', 'b', 'c', 'd', 'e']
2525

2626
rows = len(data)
2727
ind = np.arange(len(col_labels)) + 0.3 # the x locations for the groups
@@ -33,11 +33,11 @@ def test_bbox_inches_tight():
3333
for row in range(rows):
3434
ax.bar(ind, data[row], width, bottom=yoff, align='edge', color='b')
3535
yoff = yoff + data[row]
36-
cell_text.append([''])
36+
cell_text.append(['x'])
3737
plt.xticks([])
3838
plt.xlim(0, 5)
39-
plt.legend([''] * 5, loc=(1.2, 0.2))
40-
fig.legend([''] * 5, bbox_to_anchor=(0, 0.2), loc='lower left')
39+
plt.legend(['a', 'b', 'c', 'd', 'e'], loc=(1.2, 0.2))
40+
fig.legend(['a', 'b', 'c', 'd', 'e'], bbox_to_anchor=(0, 0.2), loc='lower left')
4141
# Add a table at the bottom of the axes
4242
cell_text.reverse()
4343
plt.table(cellText=cell_text, rowLabels=row_labels, colLabels=col_labels,

lib/matplotlib/tests/test_lines.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,13 @@ def test_marker_as_markerstyle():
332332

333333

334334
@image_comparison(['striped_line.png'], remove_text=True, style='mpl20')
335-
def test_striped_lines():
335+
def test_striped_lines(text_placeholders):
336336
rng = np.random.default_rng(19680801)
337337
_, ax = plt.subplots()
338338
ax.plot(rng.uniform(size=12), color='orange', gapcolor='blue',
339-
linestyle='--', lw=5, label=' ')
339+
linestyle='--', lw=5, label='blue in orange')
340340
ax.plot(rng.uniform(size=12), color='red', gapcolor='black',
341-
linestyle=(0, (2, 5, 4, 2)), lw=5, label=' ', alpha=0.5)
341+
linestyle=(0, (2, 5, 4, 2)), lw=5, label='black in red', alpha=0.5)
342342
ax.legend(handlelength=5)
343343

344344

lib/matplotlib/tests/test_patheffects.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ def test_collection():
135135
'edgecolor': 'blue'})
136136

137137

138-
@image_comparison(['tickedstroke'], remove_text=True, extensions=['png'],
139-
tol=0.22) # Increased tolerance due to fixed clipping.
140-
def test_tickedstroke():
138+
@image_comparison(['tickedstroke.png'], remove_text=True, style='mpl20')
139+
def test_tickedstroke(text_placeholders):
141140
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4))
142141
path = Path.unit_circle()
143142
patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[
@@ -149,13 +148,13 @@ def test_tickedstroke():
149148
ax1.set_xlim(-2, 2)
150149
ax1.set_ylim(-2, 2)
151150

152-
ax2.plot([0, 1], [0, 1], label=' ',
151+
ax2.plot([0, 1], [0, 1], label='C0',
153152
path_effects=[path_effects.withTickedStroke(spacing=7,
154153
angle=135)])
155154
nx = 101
156155
x = np.linspace(0.0, 1.0, nx)
157156
y = 0.3 * np.sin(x * 8) + 0.4
158-
ax2.plot(x, y, label=' ', path_effects=[path_effects.withTickedStroke()])
157+
ax2.plot(x, y, label='C1', path_effects=[path_effects.withTickedStroke()])
159158

160159
ax2.legend()
161160

0 commit comments

Comments
 (0)