Skip to content

Commit 7c0d03a

Browse files
authored
Merge pull request matplotlib#23692 from stefmolin/get-tick-params
ENH: Add `Axes.get_tick_params()` method.
2 parents cb7d7cd + 6920dc8 commit 7c0d03a

File tree

5 files changed

+139
-10
lines changed

5 files changed

+139
-10
lines changed

doc/api/axis_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Ticks, tick labels and Offset text
100100
Axis.get_offset_text
101101

102102
Axis.get_tick_padding
103+
Axis.get_tick_params
103104
Axis.get_ticklabels
104105
Axis.get_ticklines
105106
Axis.get_ticklocs
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
View current appearance settings for ticks, tick labels, and gridlines
2+
----------------------------------------------------------------------
3+
4+
The new `~matplotlib.axis.Axis.get_tick_params` method can be used to
5+
retrieve the appearance settings that will be applied to any
6+
additional ticks, tick labels, and gridlines added to the plot:
7+
8+
.. code-block:: pycon
9+
10+
>>> import matplotlib.pyplot as plt
11+
12+
>>> fig, ax = plt.subplots()
13+
>>> ax.yaxis.set_tick_params(labelsize=30, labelcolor='red',
14+
... direction='out', which='major')
15+
>>> ax.yaxis.get_tick_params(which='major')
16+
{'direction': 'out',
17+
'left': True,
18+
'right': False,
19+
'labelleft': True,
20+
'labelright': False,
21+
'gridOn': False,
22+
'labelsize': 30,
23+
'labelcolor': 'red'}
24+
>>> ax.yaxis.get_tick_params(which='minor')
25+
{'left': True,
26+
'right': False,
27+
'labelleft': True,
28+
'labelright': False,
29+
'gridOn': False}

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3407,7 +3407,8 @@ def tick_params(self, axis='both', **kwargs):
34073407
Change the appearance of ticks, tick labels, and gridlines.
34083408
34093409
Tick properties that are not explicitly set using the keyword
3410-
arguments remain unchanged unless *reset* is True.
3410+
arguments remain unchanged unless *reset* is True. For the current
3411+
style settings, see `.Axis.get_tick_params`.
34113412
34123413
Parameters
34133414
----------

lib/matplotlib/axis.py

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ def set_tick_params(self, which='major', reset=False, **kwargs):
916916
917917
For documentation of keyword arguments, see
918918
:meth:`matplotlib.axes.Axes.tick_params`.
919+
920+
See Also
921+
--------
922+
.Axis.get_tick_params
923+
View the current style settings for ticks, ticklabels, and
924+
gridlines.
919925
"""
920926
_api.check_in_list(['major', 'minor', 'both'], which=which)
921927
kwtrans = self._translate_tick_params(kwargs)
@@ -949,8 +955,62 @@ def set_tick_params(self, which='major', reset=False, **kwargs):
949955

950956
self.stale = True
951957

958+
def get_tick_params(self, which='major'):
959+
"""
960+
Get appearance parameters for ticks, ticklabels, and gridlines.
961+
962+
.. versionadded:: 3.7
963+
964+
Parameters
965+
----------
966+
which : {'major', 'minor'}, default: 'major'
967+
The group of ticks for which the parameters are retrieved.
968+
969+
Returns
970+
-------
971+
dict
972+
Properties for styling tick elements added to the axis.
973+
974+
Notes
975+
-----
976+
This method returns the appearance parameters for styling *new*
977+
elements added to this axis and may be different from the values
978+
on current elements if they were modified directly by the user
979+
(e.g., via ``set_*`` methods on individual tick objects).
980+
981+
Examples
982+
--------
983+
::
984+
985+
>>> ax.yaxis.set_tick_params(labelsize=30, labelcolor='red',
986+
direction='out', which='major')
987+
>>> ax.yaxis.get_tick_params(which='major')
988+
{'direction': 'out',
989+
'left': True,
990+
'right': False,
991+
'labelleft': True,
992+
'labelright': False,
993+
'gridOn': False,
994+
'labelsize': 30,
995+
'labelcolor': 'red'}
996+
>>> ax.yaxis.get_tick_params(which='minor')
997+
{'left': True,
998+
'right': False,
999+
'labelleft': True,
1000+
'labelright': False,
1001+
'gridOn': False}
1002+
1003+
1004+
"""
1005+
_api.check_in_list(['major', 'minor'], which=which)
1006+
if which == 'major':
1007+
return self._translate_tick_params(
1008+
self._major_tick_kw, reverse=True
1009+
)
1010+
return self._translate_tick_params(self._minor_tick_kw, reverse=True)
1011+
9521012
@staticmethod
953-
def _translate_tick_params(kw):
1013+
def _translate_tick_params(kw, reverse=False):
9541014
"""
9551015
Translate the kwargs supported by `.Axis.set_tick_params` to kwargs
9561016
supported by `.Tick._apply_params`.
@@ -961,9 +1021,12 @@ def _translate_tick_params(kw):
9611021
9621022
Returns a new dict of translated kwargs.
9631023
964-
Note: The input *kwargs* are currently modified, but that's ok for
965-
the only caller.
1024+
Note: Use reverse=True to translate from those supported by
1025+
`.Tick._apply_params` back to those supported by
1026+
`.Axis.set_tick_params`.
9661027
"""
1028+
kw_ = {**kw}
1029+
9671030
# The following lists may be moved to a more accessible location.
9681031
allowed_keys = [
9691032
'size', 'width', 'color', 'tickdir', 'pad',
@@ -988,19 +1051,27 @@ def _translate_tick_params(kw):
9881051
'labelright': 'label2On',
9891052
'labeltop': 'label2On',
9901053
}
991-
kwtrans = {newkey: kw.pop(oldkey)
992-
for oldkey, newkey in keymap.items() if oldkey in kw}
993-
if 'colors' in kw:
994-
c = kw.pop('colors')
1054+
if reverse:
1055+
kwtrans = {
1056+
oldkey: kw_.pop(newkey)
1057+
for oldkey, newkey in keymap.items() if newkey in kw_
1058+
}
1059+
else:
1060+
kwtrans = {
1061+
newkey: kw_.pop(oldkey)
1062+
for oldkey, newkey in keymap.items() if oldkey in kw_
1063+
}
1064+
if 'colors' in kw_:
1065+
c = kw_.pop('colors')
9951066
kwtrans['color'] = c
9961067
kwtrans['labelcolor'] = c
9971068
# Maybe move the checking up to the caller of this method.
998-
for key in kw:
1069+
for key in kw_:
9991070
if key not in allowed_keys:
10001071
raise ValueError(
10011072
"keyword %s is not recognized; valid keywords are %s"
10021073
% (key, allowed_keys))
1003-
kwtrans.update(kw)
1074+
kwtrans.update(kw_)
10041075
return kwtrans
10051076

10061077
def set_clip_path(self, clippath, transform=None):

lib/matplotlib/tests/test_axes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6486,6 +6486,33 @@ def test_pandas_bar_align_center(pd):
64866486
fig.canvas.draw()
64876487

64886488

6489+
def test_axis_get_tick_params():
6490+
axis = plt.subplot().yaxis
6491+
initial_major_style_translated = {**axis.get_tick_params(which='major')}
6492+
initial_minor_style_translated = {**axis.get_tick_params(which='minor')}
6493+
6494+
translated_major_kw = axis._translate_tick_params(
6495+
axis._major_tick_kw, reverse=True
6496+
)
6497+
translated_minor_kw = axis._translate_tick_params(
6498+
axis._minor_tick_kw, reverse=True
6499+
)
6500+
6501+
assert translated_major_kw == initial_major_style_translated
6502+
assert translated_minor_kw == initial_minor_style_translated
6503+
axis.set_tick_params(labelsize=30, labelcolor='red',
6504+
direction='out', which='both')
6505+
6506+
new_major_style_translated = {**axis.get_tick_params(which='major')}
6507+
new_minor_style_translated = {**axis.get_tick_params(which='minor')}
6508+
new_major_style = axis._translate_tick_params(new_major_style_translated)
6509+
new_minor_style = axis._translate_tick_params(new_minor_style_translated)
6510+
assert initial_major_style_translated != new_major_style_translated
6511+
assert axis._major_tick_kw == new_major_style
6512+
assert initial_minor_style_translated != new_minor_style_translated
6513+
assert axis._minor_tick_kw == new_minor_style
6514+
6515+
64896516
def test_axis_set_tick_params_labelsize_labelcolor():
64906517
# Tests fix for issue 4346
64916518
axis_1 = plt.subplot()

0 commit comments

Comments
 (0)