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

Skip to content

Commit 5c13fb4

Browse files
authored
Merge pull request #17348 from QuLogic/tickspace-no-tick
Avoid creating a Tick in Axis.get_tick_space.
2 parents 661100a + 222b3ba commit 5c13fb4

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/matplotlib/axis.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def _translate_tick_kw(kw):
880880
raise ValueError(
881881
"keyword %s is not recognized; valid keywords are %s"
882882
% (key, kwkeys))
883-
kwtrans.update(kw)
883+
kwtrans.update(kw)
884884
return kwtrans
885885

886886
def set_clip_path(self, clippath, transform=None):
@@ -1317,6 +1317,18 @@ def _get_tick(self, major):
13171317
"""Return the default tick instance."""
13181318
raise NotImplementedError('derived must override')
13191319

1320+
def _get_tick_label_size(self, axis_name):
1321+
"""
1322+
Return the text size of tick labels for this Axis.
1323+
1324+
This is a convenience function to avoid having to create a `Tick` in
1325+
`.get_tick_space`, since it is expensive.
1326+
"""
1327+
tick_kw = self._major_tick_kw
1328+
size = tick_kw.get('labelsize',
1329+
mpl.rcParams[f'{axis_name}tick.labelsize'])
1330+
return mtext.FontProperties(size).get_size_in_points()
1331+
13201332
def _copy_tick_props(self, src, dest):
13211333
"""Copy the properties from *src* tick to *dest* tick."""
13221334
if src is None or dest is None:
@@ -2214,10 +2226,9 @@ def set_default_intervals(self):
22142226
def get_tick_space(self):
22152227
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
22162228
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
2217-
tick = self._get_tick(True)
22182229
# There is a heuristic here that the aspect ratio of tick text
22192230
# is no more than 3:1
2220-
size = tick.label1.get_size() * 3
2231+
size = self._get_tick_label_size('x') * 3
22212232
if size > 0:
22222233
return int(np.floor(length / size))
22232234
else:
@@ -2500,9 +2511,8 @@ def set_default_intervals(self):
25002511
def get_tick_space(self):
25012512
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
25022513
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
2503-
tick = self._get_tick(True)
25042514
# Having a spacing of at least 2 just looks good.
2505-
size = tick.label1.get_size() * 2.0
2515+
size = self._get_tick_label_size('y') * 2
25062516
if size > 0:
25072517
return int(np.floor(length / size))
25082518
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)