Skip to content

Commit cedd990

Browse files
committed
DOC: Document tick objects
Closes matplotlib#31697
1 parent 17dbcbe commit cedd990

4 files changed

Lines changed: 71 additions & 3 deletions

File tree

doc/api/axes_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ Aspect ratio
395395
Axes.set_adjustable
396396
Axes.get_adjustable
397397

398+
.. _axes-api-ticks:
399+
398400
Ticks and tick labels
399401
---------------------
400402

doc/api/axis_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Axis Label
8080
Axis.get_label_position
8181
Axis.get_label_text
8282

83+
.. _axis-api-ticks:
84+
8385
Ticks, tick labels and Offset text
8486
----------------------------------
8587

galleries/users_explain/axes/axes_ticks.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ def fmt_two_digits(x, pos):
227227

228228
# %%
229229
#
230+
# .. _axes_ticks_styling:
231+
#
230232
# Styling ticks (tick parameters)
231233
# ===============================
232234
#
@@ -273,3 +275,53 @@ def fmt_two_digits(x, pos):
273275
grid_color='none')
274276
ax.tick_params(axis='x', color='m', length=4, direction='in', width=4,
275277
labelcolor='g', grid_color='b')
278+
279+
# %%
280+
#
281+
# .. _axes-tick-objects:
282+
#
283+
# Tick objects
284+
# ============
285+
#
286+
# .. warning::
287+
#
288+
# Ticks are managed through the autoscaling / view limit mechanism, which may
289+
# create, move and delete ticks as necessary.
290+
#
291+
# Working with tick instances should only be an option of last resort and requires
292+
# careful handling to not accidentally overwrite any manual changes through this
293+
# mechanism.
294+
#
295+
# If a tick configuration can be achieved through `.Axes.tick_params` (see
296+
# :ref:`axes-ticks-styling`), that approach should be preferred.
297+
#
298+
# On the technical level, ticks are realized through `.Tick` objects. They consist of
299+
#
300+
# - ``tick1line`` / ``tick2line`` for the tick lines on either side of the axis.
301+
# - ``label1`` / ``label2`` for the tick labels on either side of the axis.
302+
# - ``gridline`` for the grid line.
303+
#
304+
# These objects are publicly accessible through :ref:`Axes methods <axes-api-ticks>`
305+
# and :ref:`Axis methods <axis-api-ticks>` and allow extreme customization such as
306+
# coloring a specific tick line or tick label separately.
307+
308+
x = 1.1 * np.exp(-3 * np.random.random(500))
309+
y = np.random.random(500)
310+
311+
fig, ax = plt.subplots()
312+
313+
ax.plot(x, y, 'o')
314+
ax.grid()
315+
ticks = ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1, 1.2])
316+
ticks[5].label1.set_color("red")
317+
ticks[5].tick1line.set_markeredgecolor("red")
318+
ticks[5].gridline.set(visible=True, linestyle=':', linewidth=1.5, color='red')
319+
320+
# %%
321+
# Because of the managed nature of ticks, such operations only make sense for static
322+
# output (e.g., when using the jupyter inline backend or saving the figure to a file)
323+
# or when you are sure that the view limits of the axis will not change (e.g., when
324+
# using a fixed locator).
325+
#
326+
# In contrast, `.Axes.tick_params` configures the default properties so that they are
327+
# applied to all current and future ticks.

lib/matplotlib/axis.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ class Tick(martist.Artist):
5757
label2 : `~matplotlib.text.Text`
5858
The right/top tick label.
5959
60+
Notes
61+
-----
62+
Ticks are managed though the autoscaling / view limit mechanism and therefore
63+
need very careful handling when explicitly accessed, see :ref:`axes-tick-objects`.
64+
65+
The tick lines are implemented as `~matplotlib.lines.Line2D` instances using the
66+
`~.matplotlib.markers` TICKLEFT, TICKRIGHT, TICKDOWN, TICKUP. Therefore,
67+
properties are controlled via the marker, e.g. for the color
68+
`.Line2D.set_markeredgecolor` and not `.Line2D.set_color`.
6069
"""
6170
def __init__(
6271
self, axes, loc, *,
@@ -84,9 +93,12 @@ def __init__(
8493
**kwargs, # Other Line2D kwargs applied to gridlines.
8594
):
8695
"""
87-
bbox is the Bound2D bounding box in display coords of the Axes
88-
loc is the tick location in data coords
89-
size is the tick size in points
96+
Parameters
97+
----------
98+
loc
99+
The tick location in data coords.
100+
size
101+
The tick size in points.
90102
"""
91103
super().__init__()
92104

0 commit comments

Comments
 (0)