Skip to content

Deprecate Tick.apply_tickdir. #19655

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
Mar 10, 2021
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
2 changes: 0 additions & 2 deletions doc/api/axis_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ specify a matching series of labels. Calling ``set_ticks`` makes a
:template: autosummary.rst
:nosignatures:


Tick.apply_tickdir
Tick.get_loc
Tick.get_pad
Tick.get_pad_pixels
Expand Down
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/19655-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``Tick.apply_tickdir`` is deprecated
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``apply_tickdir`` didn't actually update the tick markers on the existing
Line2D objects used to draw the ticks; use `.Axis.set_tick_params` instead.
74 changes: 34 additions & 40 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ def __init__(self, axes, loc, label=None,
grid_alpha = mpl.rcParams["grid.alpha"]
grid_kw = {k[5:]: v for k, v in kw.items()}

self.apply_tickdir(tickdir)

self.tick1line = mlines.Line2D(
[], [],
color=color, linestyle="none", zorder=zorder, visible=tick1On,
Expand All @@ -174,6 +172,9 @@ def __init__(self, axes, loc, label=None,
self.label2 = mtext.Text(
np.nan, np.nan,
fontsize=labelsize, color=labelcolor, visible=label2On)

self._apply_tickdir(tickdir)

for meth, attr in [("_get_tick1line", "tick1line"),
("_get_tick2line", "tick2line"),
("_get_gridline", "gridline"),
Expand Down Expand Up @@ -209,15 +210,22 @@ def _set_labelrotation(self, labelrotation):
_api.check_in_list(['auto', 'default'], labelrotation=mode)
self._labelrotation = (mode, angle)

def apply_tickdir(self, tickdir):
def _apply_tickdir(self, tickdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've dug into the logic: Can you extend the docstring to explain what this does/not, and how it has to be used with _apply_params. (Or if thats the case: that it only works when called from within _apply_params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I further simplified the coordination between apply_params and apply_tickdir by getting rid of _tickmarkers (directly setting them in apply_tickdir) and letting apply_params be in charge of staleness.
Also extended the comment to explain what _appky_tickdir is responsible for.

"""Set tick direction. Valid values are 'out', 'in', 'inout'."""
# This method is responsible for updating `_pad`, and, in subclasses,
# for setting the tick{1,2}line markers as well. From the user
# perspective this should always be called though _apply_params, which
# further updates ticklabel positions using the new pads.
if tickdir is None:
tickdir = mpl.rcParams[f'{self.__name__}.direction']
_api.check_in_list(['in', 'out', 'inout'], tickdir=tickdir)
self._tickdir = tickdir
self._pad = self._base_pad + self.get_tick_padding()

@_api.deprecated("3.5", alternative="axis.set_tick_params")
def apply_tickdir(self, tickdir):
self._apply_tickdir()
self.stale = True
# Subclass overrides should compute _tickmarkers as appropriate here.

def get_tickdir(self):
return self._tickdir
Expand Down Expand Up @@ -363,15 +371,13 @@ def _apply_params(self, **kw):
# convenient to leave it here.
self._width = kw.pop('width', self._width)
self._base_pad = kw.pop('pad', self._base_pad)
# apply_tickdir uses _size and _base_pad to make _pad,
# and also makes _tickmarkers.
self.apply_tickdir(kw.pop('tickdir', self._tickdir))
self.tick1line.set_marker(self._tickmarkers[0])
self.tick2line.set_marker(self._tickmarkers[1])
# _apply_tickdir uses _size and _base_pad to make _pad, and also
# sets the ticklines markers.
self._apply_tickdir(kw.pop('tickdir', self._tickdir))
for line in (self.tick1line, self.tick2line):
line.set_markersize(self._size)
line.set_markeredgewidth(self._width)
# _get_text1_transform uses _pad from apply_tickdir.
# _get_text1_transform uses _pad from _apply_tickdir.
trans = self._get_text1_transform()[0]
self.label1.set_transform(trans)
trans = self._get_text2_transform()[0]
Expand Down Expand Up @@ -419,20 +425,13 @@ class XTick(Tick):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# x in data coords, y in axes coords
ax = self.axes
self.tick1line.set(
xdata=[0], ydata=[0],
transform=self.axes.get_xaxis_transform(which="tick1"),
marker=self._tickmarkers[0],
)
data=([0], [0]), transform=ax.get_xaxis_transform("tick1"))
self.tick2line.set(
xdata=[0], ydata=[1],
transform=self.axes.get_xaxis_transform(which="tick2"),
marker=self._tickmarkers[1],
)
data=([0], [1]), transform=ax.get_xaxis_transform("tick2"))
self.gridline.set(
xdata=[0, 0], ydata=[0, 1],
transform=self.axes.get_xaxis_transform(which="grid"),
)
data=([0, 0], [0, 1]), transform=ax.get_xaxis_transform("grid"))
# the y loc is 3 points below the min of y axis
trans, va, ha = self._get_text1_transform()
self.label1.set(
Expand All @@ -451,15 +450,16 @@ def _get_text1_transform(self):
def _get_text2_transform(self):
return self.axes.get_xaxis_text2_transform(self._pad)

def apply_tickdir(self, tickdir):
def _apply_tickdir(self, tickdir):
# docstring inherited
super().apply_tickdir(tickdir)
self._tickmarkers = {
super()._apply_tickdir(tickdir)
mark1, mark2 = {
'out': (mlines.TICKDOWN, mlines.TICKUP),
'in': (mlines.TICKUP, mlines.TICKDOWN),
'inout': ('|', '|'),
}[self._tickdir]
self.stale = True
self.tick1line.set_marker(mark1)
self.tick2line.set_marker(mark2)

def update_position(self, loc):
"""Set the location of tick in data coords with scalar *loc*."""
Expand All @@ -486,20 +486,13 @@ class YTick(Tick):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# x in axes coords, y in data coords
ax = self.axes
self.tick1line.set(
xdata=[0], ydata=[0],
transform=self.axes.get_yaxis_transform(which="tick1"),
marker=self._tickmarkers[0],
)
data=([0], [0]), transform=ax.get_yaxis_transform("tick1"))
self.tick2line.set(
xdata=[1], ydata=[0],
transform=self.axes.get_yaxis_transform(which="tick2"),
marker=self._tickmarkers[1],
)
data=([1], [0]), transform=ax.get_yaxis_transform("tick2"))
self.gridline.set(
xdata=[0, 1], ydata=[0, 0],
transform=self.axes.get_yaxis_transform(which="grid"),
)
data=([0, 1], [0, 0]), transform=ax.get_yaxis_transform("grid"))
# the y loc is 3 points below the min of y axis
trans, va, ha = self._get_text1_transform()
self.label1.set(
Expand All @@ -518,15 +511,16 @@ def _get_text1_transform(self):
def _get_text2_transform(self):
return self.axes.get_yaxis_text2_transform(self._pad)

def apply_tickdir(self, tickdir):
def _apply_tickdir(self, tickdir):
# docstring inherited
super().apply_tickdir(tickdir)
self._tickmarkers = {
super()._apply_tickdir(tickdir)
mark1, mark2 = {
'out': (mlines.TICKLEFT, mlines.TICKRIGHT),
'in': (mlines.TICKRIGHT, mlines.TICKLEFT),
'inout': ('_', '_'),
}[self._tickdir]
self.stale = True
self.tick1line.set_marker(mark1)
self.tick2line.set_marker(mark2)

def update_position(self, loc):
"""Set the location of tick in data coords with scalar *loc*."""
Expand Down