|
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,47 @@ 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 | + def perf_counter(): |
| 1118 | + return next(counter) |
| 1119 | + monkeypatch.setattr(time, 'perf_counter', perf_counter) |
| 1120 | + |
1113 | 1121 | fig, ax = plt.subplots()
|
1114 | 1122 | 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) |
| 1123 | + |
| 1124 | + # Place line across all possible legend locations. |
| 1125 | + x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5] |
| 1126 | + y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5] |
| 1127 | + ax.plot(x, y, 'o-', label='line') |
| 1128 | + |
1117 | 1129 | with rc_context({'legend.loc': 'best'}):
|
1118 | 1130 | legend = ax.legend()
|
1119 |
| - with pytest.warns(UserWarning) as records: |
| 1131 | + with pytest.warns(UserWarning, |
| 1132 | + match='Creating legend with loc="best" can be slow with large ' |
| 1133 | + 'amounts of data.') as records: |
1120 | 1134 | fig.draw_artist(legend) # Don't bother drawing the lines -- it's slow.
|
1121 | 1135 | # The _find_best_position method of Legend is called twice, duplicating
|
1122 | 1136 | # the warning message.
|
1123 | 1137 | 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 | 1138 |
|
1129 | 1139 |
|
1130 |
| -def test_no_warn_big_data_when_loc_specified(): |
| 1140 | +def test_no_warn_big_data_when_loc_specified(monkeypatch): |
| 1141 | + # Force _find_best_position to think it took a long time. |
| 1142 | + counter = itertools.count(0, step=1.5) |
| 1143 | + def perf_counter(): |
| 1144 | + return next(counter) |
| 1145 | + monkeypatch.setattr(time, 'perf_counter', perf_counter) |
| 1146 | + |
1131 | 1147 | fig, ax = plt.subplots()
|
1132 | 1148 | fig.canvas.draw()
|
1133 |
| - for idx in range(1000): |
1134 |
| - ax.plot(np.arange(5000), label=idx) |
| 1149 | + |
| 1150 | + # Place line across all possible legend locations. |
| 1151 | + x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5] |
| 1152 | + y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5] |
| 1153 | + ax.plot(x, y, 'o-', label='line') |
| 1154 | + |
1135 | 1155 | legend = ax.legend('best')
|
1136 | 1156 | fig.draw_artist(legend) # Check that no warning is emitted.
|
1137 | 1157 |
|
|
0 commit comments