|
1 | 1 | import collections |
| 2 | +import itertools |
2 | 3 | import platform |
| 4 | +import time |
3 | 5 | from unittest import mock |
4 | 6 | import warnings |
5 | 7 |
|
@@ -1109,29 +1111,43 @@ def test_usetex_no_warn(caplog): |
1109 | 1111 | assert "Font family ['serif'] not found." not in caplog.text |
1110 | 1112 |
|
1111 | 1113 |
|
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 | + |
1113 | 1119 | fig, ax = plt.subplots() |
1114 | 1120 | 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 | + |
1117 | 1127 | with rc_context({'legend.loc': 'best'}): |
1118 | 1128 | 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: |
1120 | 1132 | fig.draw_artist(legend) # Don't bother drawing the lines -- it's slow. |
1121 | 1133 | # The _find_best_position method of Legend is called twice, duplicating |
1122 | 1134 | # the warning message. |
1123 | 1135 | 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.') |
1128 | 1136 |
|
1129 | 1137 |
|
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 | + |
1131 | 1143 | fig, ax = plt.subplots() |
1132 | 1144 | 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 | + |
1135 | 1151 | legend = ax.legend('best') |
1136 | 1152 | fig.draw_artist(legend) # Check that no warning is emitted. |
1137 | 1153 |
|
|
0 commit comments