Skip to content

Avoid creating a Tick in Axis.get_tick_space. #17348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def _translate_tick_kw(kw):
raise ValueError(
"keyword %s is not recognized; valid keywords are %s"
% (key, kwkeys))
kwtrans.update(kw)
kwtrans.update(kw)
return kwtrans

def set_clip_path(self, clippath, transform=None):
Expand Down Expand Up @@ -1315,6 +1315,18 @@ def _get_tick(self, major):
"""Return the default tick instance."""
raise NotImplementedError('derived must override')

def _get_tick_label_size(self, axis_name):
"""
Return the text size of tick labels for this Axis.

This is a convenience function to avoid having to create a `Tick` in
`.get_tick_space`, since it is expensive.
"""
tick_kw = self._major_tick_kw
size = tick_kw.get('labelsize',
mpl.rcParams[f'{axis_name}tick.labelsize'])
return mtext.FontProperties(size).get_size_in_points()

def _copy_tick_props(self, src, dest):
"""Copy the properties from *src* tick to *dest* tick."""
if src is None or dest is None:
Expand Down Expand Up @@ -2186,10 +2198,9 @@ def set_default_intervals(self):
def get_tick_space(self):
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
tick = self._get_tick(True)
# There is a heuristic here that the aspect ratio of tick text
# is no more than 3:1
size = tick.label1.get_size() * 3
size = self._get_tick_label_size('x') * 3
if size > 0:
return int(np.floor(length / size))
else:
Expand Down Expand Up @@ -2472,9 +2483,8 @@ def set_default_intervals(self):
def get_tick_space(self):
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
tick = self._get_tick(True)
# Having a spacing of at least 2 just looks good.
size = tick.label1.get_size() * 2.0
size = self._get_tick_label_size('y') * 2
if size > 0:
return int(np.floor(length / size))
else:
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from pathlib import Path
import platform
from types import SimpleNamespace
import warnings
try:
from contextlib import nullcontext
Expand Down Expand Up @@ -533,3 +534,17 @@ def test_removed_axis():
fig, axs = plt.subplots(2, sharex=True)
axs[0].remove()
fig.canvas.draw()


@pytest.mark.style('mpl20')
def test_picking_does_not_stale():
fig, ax = plt.subplots()
col = ax.scatter([0], [0], [1000], picker=True)
fig.canvas.draw()
assert not fig.stale

mouse_event = SimpleNamespace(x=ax.bbox.x0 + ax.bbox.width / 2,
y=ax.bbox.y0 + ax.bbox.height / 2,
inaxes=ax, guiEvent=None)
fig.pick(mouse_event)
assert not fig.stale