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

Lines changed: 900 additions & 369 deletions
Loading
Loading
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 4 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 8 additions & 7 deletions
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,

0 commit comments

Comments
 (0)