Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d6c12a

Browse files
committed
Make slowness warning for legend(loc="best") more accurate.
... by actually timing the call duration. Locally I can best-locatelegends even with plots with hundreds of thousands of points basicallyinstantly, so the old warning was spurious.The new test is obviously a bit brittle because it depends on how fastthe machine running it is. It's also slower than the test before(intentionally, because now you *actually* need a slow-to-place legendto trigger the warning).The warning is only emitted after the legend has been placed, but thatseems fine -- if the best-placement is so slow that you ctrl-c theprocess, you'll have a traceback anyways. Also, spawning a separatethread to always emit the warning after exactly 5s will likely just makethings worse performance-wise on average.
1 parent43e7d3b commit7d6c12a

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

‎lib/matplotlib/legend.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323

2424
importlogging
25+
importtime
2526

2627
importnumpyasnp
2728

@@ -1112,13 +1113,9 @@ def _find_best_position(self, width, height, renderer, consider=None):
11121113
# should always hold because function is only called internally
11131114
assertself.isaxes
11141115

1116+
start_time=time.perf_counter()
1117+
11151118
verts,bboxes,lines,offsets=self._auto_legend_data()
1116-
ifself._loc_used_defaultandverts.shape[0]>200000:
1117-
# this size results in a 3+ second render time on a good machine
1118-
cbook._warn_external(
1119-
'Creating legend with loc="best" can be slow with large '
1120-
'amounts of data.'
1121-
)
11221119

11231120
bbox=Bbox.from_bounds(0,0,width,height)
11241121
ifconsiderisNone:
@@ -1145,6 +1142,12 @@ def _find_best_position(self, width, height, renderer, consider=None):
11451142
candidates.append((badness,idx, (l,b)))
11461143

11471144
_,_, (l,b)=min(candidates)
1145+
1146+
ifself._loc_used_defaultandtime.perf_counter()-start_time>1:
1147+
cbook._warn_external(
1148+
'Creating legend with loc="best" can be slow with large '
1149+
'amounts of data.')
1150+
11481151
returnl,b
11491152

11501153
defcontains(self,event):

‎lib/matplotlib/tests/test_legend.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -550,24 +550,28 @@ def test_alpha_handles():
550550

551551
deftest_warn_big_data_best_loc():
552552
fig,ax=plt.subplots()
553-
ax.plot(np.arange(200001),label='Is this big data?')
553+
fig.canvas.draw()# So that we can call draw_artist later.
554+
foridxinrange(1000):
555+
ax.plot(np.arange(5000),label=idx)
556+
withrc_context({'legend.loc':'best'}):
557+
legend=ax.legend()
554558
withpytest.warns(UserWarning)asrecords:
555-
withrc_context({'legend.loc':'best'}):
556-
ax.legend()
557-
fig.canvas.draw()
559+
fig.draw_artist(legend)# Don't bother drawing the lines -- it's slow.
558560
# The _find_best_position method of Legend is called twice, duplicating
559561
# the warning message.
560562
assertlen(records)==2
561563
forrecordinrecords:
562564
assertstr(record.message)== (
563-
'Creating legend with loc="best" can be slow with large'
564-
'amounts of data.')
565+
'Creating legend with loc="best" can be slow with large'
566+
'amounts of data.')
565567

566568

567569
deftest_no_warn_big_data_when_loc_specified():
568570
fig,ax=plt.subplots()
569-
ax.plot(np.arange(200001),label='Is this big data?')
571+
fig.canvas.draw()
572+
foridxinrange(1000):
573+
ax.plot(np.arange(5000),label=idx)
574+
legend=ax.legend('best')
570575
withpytest.warns(None)asrecords:
571-
ax.legend('best')
572-
fig.canvas.draw()
576+
fig.draw_artist(legend)
573577
assertlen(records)==0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp