Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cc6610

Browse files
committed
TST: Improve test for Legend(loc='best') warning
By patching the timer instead of using actually large data, we can both a) speed up these tests (~7.5s vs <0.2s for both), and b) consistently trigger the warning even on systems which are fast (such as the M1 systems on Cirrus.) Also, copy the test data from `test_legend_auto3`, which correctly hits all candidate locations for the 'best' legend locator without having to fill up the entire Axes with data.
1 parent 3516356 commit 3cc6610

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

lib/matplotlib/tests/test_legend.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import collections
2+
import itertools
23
import platform
4+
import time
35
from unittest import mock
46
import warnings
57

@@ -1109,29 +1111,43 @@ def test_usetex_no_warn(caplog):
11091111
assert "Font family ['serif'] not found." not in caplog.text
11101112

11111113

1112-
def test_warn_big_data_best_loc():
1114+
def test_warn_big_data_best_loc(monkeypatch):
1115+
# Force _find_best_position to think it took a long time.
1116+
counter = itertools.count(0, step=1.5)
1117+
monkeypatch.setattr(time, 'perf_counter', lambda: next(counter))
1118+
11131119
fig, ax = plt.subplots()
11141120
fig.canvas.draw() # So that we can call draw_artist later.
1115-
for idx in range(1000):
1116-
ax.plot(np.arange(5000), label=idx)
1121+
1122+
# Place line across all possible legend locations.
1123+
x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5]
1124+
y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5]
1125+
ax.plot(x, y, 'o-', label='line')
1126+
11171127
with rc_context({'legend.loc': 'best'}):
11181128
legend = ax.legend()
1119-
with pytest.warns(UserWarning) as records:
1129+
with pytest.warns(UserWarning,
1130+
match='Creating legend with loc="best" can be slow with large '
1131+
'amounts of data.') as records:
11201132
fig.draw_artist(legend) # Don't bother drawing the lines -- it's slow.
11211133
# The _find_best_position method of Legend is called twice, duplicating
11221134
# the warning message.
11231135
assert len(records) == 2
1124-
for record in records:
1125-
assert str(record.message) == (
1126-
'Creating legend with loc="best" can be slow with large '
1127-
'amounts of data.')
11281136

11291137

1130-
def test_no_warn_big_data_when_loc_specified():
1138+
def test_no_warn_big_data_when_loc_specified(monkeypatch):
1139+
# Force _find_best_position to think it took a long time.
1140+
counter = itertools.count(0, step=1.5)
1141+
monkeypatch.setattr(time, 'perf_counter', lambda: next(counter))
1142+
11311143
fig, ax = plt.subplots()
11321144
fig.canvas.draw()
1133-
for idx in range(1000):
1134-
ax.plot(np.arange(5000), label=idx)
1145+
1146+
# Place line across all possible legend locations.
1147+
x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5]
1148+
y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5]
1149+
ax.plot(x, y, 'o-', label='line')
1150+
11351151
legend = ax.legend('best')
11361152
fig.draw_artist(legend) # Check that no warning is emitted.
11371153

0 commit comments

Comments
 (0)