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

Skip to content

Commit 222b3ba

Browse files
committed
Avoid creating a Tick in Axis.get_tick_space.
Doing so is expensive, and triggers the figure to be marked stale. We don't need a whole tick, just the font size that it will eventually use, so calculate that directly.
1 parent 9909aeb commit 222b3ba

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/matplotlib/axis.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,18 @@ def _get_tick(self, major):
13151315
"""Return the default tick instance."""
13161316
raise NotImplementedError('derived must override')
13171317

1318+
def _get_tick_label_size(self, axis_name):
1319+
"""
1320+
Return the text size of tick labels for this Axis.
1321+
1322+
This is a convenience function to avoid having to create a `Tick` in
1323+
`.get_tick_space`, since it is expensive.
1324+
"""
1325+
tick_kw = self._major_tick_kw
1326+
size = tick_kw.get('labelsize',
1327+
mpl.rcParams[f'{axis_name}tick.labelsize'])
1328+
return mtext.FontProperties(size).get_size_in_points()
1329+
13181330
def _copy_tick_props(self, src, dest):
13191331
"""Copy the properties from *src* tick to *dest* tick."""
13201332
if src is None or dest is None:
@@ -2186,10 +2198,9 @@ def set_default_intervals(self):
21862198
def get_tick_space(self):
21872199
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
21882200
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
2189-
tick = self._get_tick(True)
21902201
# There is a heuristic here that the aspect ratio of tick text
21912202
# is no more than 3:1
2192-
size = tick.label1.get_size() * 3
2203+
size = self._get_tick_label_size('x') * 3
21932204
if size > 0:
21942205
return int(np.floor(length / size))
21952206
else:
@@ -2472,9 +2483,8 @@ def set_default_intervals(self):
24722483
def get_tick_space(self):
24732484
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
24742485
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
2475-
tick = self._get_tick(True)
24762486
# Having a spacing of at least 2 just looks good.
2477-
size = tick.label1.get_size() * 2.0
2487+
size = self._get_tick_label_size('y') * 2
24782488
if size > 0:
24792489
return int(np.floor(length / size))
24802490
else:

lib/matplotlib/tests/test_figure.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime
22
from pathlib import Path
33
import platform
4+
from types import SimpleNamespace
45
import warnings
56
try:
67
from contextlib import nullcontext
@@ -533,3 +534,17 @@ def test_removed_axis():
533534
fig, axs = plt.subplots(2, sharex=True)
534535
axs[0].remove()
535536
fig.canvas.draw()
537+
538+
539+
@pytest.mark.style('mpl20')
540+
def test_picking_does_not_stale():
541+
fig, ax = plt.subplots()
542+
col = ax.scatter([0], [0], [1000], picker=True)
543+
fig.canvas.draw()
544+
assert not fig.stale
545+
546+
mouse_event = SimpleNamespace(x=ax.bbox.x0 + ax.bbox.width / 2,
547+
y=ax.bbox.y0 + ax.bbox.height / 2,
548+
inaxes=ax, guiEvent=None)
549+
fig.pick(mouse_event)
550+
assert not fig.stale

0 commit comments

Comments
 (0)