Skip to content

Commit c1a2ec4

Browse files
authored
Merge pull request matplotlib#29908 from QuLogic/legend-placeholders
TST: Use text placeholders for empty legends
2 parents 6d98336 + 7cdc74f commit c1a2ec4

File tree

12 files changed

+921
-390
lines changed

12 files changed

+921
-390
lines changed
Loading
Binary file not shown.
Binary file not shown.
Loading

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

+900-369
Loading
Loading
Loading

lib/matplotlib/tests/test_axes.py

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

46074607

46084608
@image_comparison(['stem.png'], style='mpl20', remove_text=True)
4609-
def test_stem():
4609+
def test_stem(text_placeholders):
46104610
x = np.linspace(0.1, 2 * np.pi, 100)
46114611

46124612
fig, ax = plt.subplots()
4613-
# Label is a single space to force a legend to be drawn, but to avoid any
4614-
# text being drawn
46154613
ax.stem(x, np.cos(x),
4616-
linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label=' ')
4614+
linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label='stem')
46174615
ax.legend()
46184616

46194617

lib/matplotlib/tests/test_backend_pdf.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,16 @@ 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"""
302302
fig = plt.figure(figsize=(1, 2))
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+
# Verify that hatches in PDFs work after empty labels. See
308+
# https://github.com/matplotlib/matplotlib/issues/4469
307309
fig.legend([a, b, a, b], ["", "", "", ""])
308310

309311

lib/matplotlib/tests/test_bbox_tight.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
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 = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
25+
row_labels = [f'{x} year' for x in (100, 50, 20, 10, 5)]
2526

2627
rows = len(data)
2728
ind = np.arange(len(col_labels)) + 0.3 # the x locations for the groups
@@ -31,13 +32,13 @@ def test_bbox_inches_tight():
3132
# the bottom values for stacked bar chart
3233
fig, ax = plt.subplots(1, 1)
3334
for row in range(rows):
34-
ax.bar(ind, data[row], width, bottom=yoff, align='edge', color='b')
35+
ax.bar(ind, data[row], width, bottom=yoff, align='edge')
3536
yoff = yoff + data[row]
36-
cell_text.append([''])
37+
cell_text.append([f'{x / 1000:1.1f}' for x in yoff])
3738
plt.xticks([])
3839
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')
40+
plt.legend(['1', '2', '3', '4', '5'], loc=(1.2, 0.2))
41+
fig.legend(['a', 'b', 'c', 'd', 'e'], bbox_to_anchor=(0, 0.2), loc='lower left')
4142
# Add a table at the bottom of the axes
4243
cell_text.reverse()
4344
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)