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

Skip to content

Commit 4a313a6

Browse files
committed
Fix get_tick_params
- show values for x axis too - fix nonfunctional test - move axis tests from test_axes.py to test_axis.py - make note in get_tick_params docstring more precise - add GridOn to set_tick_params as it's always returned by get_tick_params, so it would be illogical if you couldn't set the paramter in the setter
1 parent 07a6f66 commit 4a313a6

File tree

5 files changed

+110
-60
lines changed

5 files changed

+110
-60
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Classes derived from Axis must implement get_tick_params
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Tick parameter translation depends on the concrete axis (x or y), so any
5+
axis class derived from `~matplotlib.axis.Axis` must implement
6+
`~matplotlib.axis.Axis.get_tick_params`. This is not necessary if the new axis
7+
class is derived from `.XAxis` or `.YAxis` (as for instance the Axis classes
8+
of `mpl_toolkits.mplot3d`).

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,8 @@ def tick_params(self, axis='both', **kwargs):
33943394
Width of gridlines in points.
33953395
grid_linestyle : str
33963396
Any valid `.Line2D` line style spec.
3397+
gridOn : bool
3398+
Whether to draw the grid lines.
33973399
33983400
Examples
33993401
--------

lib/matplotlib/axis.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,7 @@ def set_tick_params(self, which='major', reset=False, **kwargs):
931931
"""
932932
Set appearance parameters for ticks, ticklabels, and gridlines.
933933
934-
For documentation of keyword arguments, see
935-
:meth:`matplotlib.axes.Axes.tick_params`.
934+
For documentation of keyword arguments, see `.Axes.tick_params`.
936935
937936
See Also
938937
--------
@@ -976,6 +975,16 @@ def get_tick_params(self, which='major'):
976975
"""
977976
Get appearance parameters for ticks, ticklabels, and gridlines.
978977
978+
.. note::
979+
This method only returns the values of the parameters *bottom*, *top*,
980+
*labelbottom*, *labeltop* or *left*, *right*, *labelleft*, *labelright*,
981+
respectively, and *girdOn* as well as all additional parameters that were
982+
set with `.Axis.set_tick_params` or methods that use it internally, such
983+
as `.Axes.tick_params`. The returned parameters may differ from the values
984+
of the current elements if they have been set by any other means (e.g.
985+
via set_* methods for individual tick objects, `.pyplot.xticks`/
986+
`.pyplot.yticks` or `.rcParams`).
987+
979988
.. versionadded:: 3.7
980989
981990
Parameters
@@ -988,13 +997,6 @@ def get_tick_params(self, which='major'):
988997
dict
989998
Properties for styling tick elements added to the axis.
990999
991-
Notes
992-
-----
993-
This method returns the appearance parameters for styling *new*
994-
elements added to this axis and may be different from the values
995-
on current elements if they were modified directly by the user
996-
(e.g., via ``set_*`` methods on individual tick objects).
997-
9981000
Examples
9991001
--------
10001002
::
@@ -1019,15 +1021,10 @@ def get_tick_params(self, which='major'):
10191021
10201022
10211023
"""
1022-
_api.check_in_list(['major', 'minor'], which=which)
1023-
if which == 'major':
1024-
return self._translate_tick_params(
1025-
self._major_tick_kw, reverse=True
1026-
)
1027-
return self._translate_tick_params(self._minor_tick_kw, reverse=True)
1024+
raise NotImplementedError('Derived must override')
10281025

10291026
@staticmethod
1030-
def _translate_tick_params(kw, reverse=False):
1027+
def _translate_tick_params(kw, reverse=False, reverse_exclude=[]):
10311028
"""
10321029
Translate the kwargs supported by `.Axis.set_tick_params` to kwargs
10331030
supported by `.Tick._apply_params`.
@@ -1036,11 +1033,22 @@ def _translate_tick_params(kw, reverse=False):
10361033
to the generic tick1, tick2 logic of the axis. Additionally, there
10371034
are some other name translations.
10381035
1039-
Returns a new dict of translated kwargs.
1036+
Parameters
1037+
----------
1038+
kw
1039+
kwargs dict to translate.
1040+
reverse
1041+
whether to translate from set_tick_params kwargs to
1042+
_apply_params kwargs or back.
1043+
reverse_exclude
1044+
list of keys to be removed from the keymap before reverse
1045+
translating. This is necessary because there are multiple keys
1046+
with the same value in keymap, depending on the axis.
10401047
1041-
Note: Use reverse=True to translate from those supported by
1042-
`.Tick._apply_params` back to those supported by
1043-
`.Axis.set_tick_params`.
1048+
Returns
1049+
-------
1050+
dict
1051+
new dict of translated kwargs
10441052
"""
10451053
kw_ = {**kw}
10461054

@@ -1069,6 +1077,8 @@ def _translate_tick_params(kw, reverse=False):
10691077
'labeltop': 'label2On',
10701078
}
10711079
if reverse:
1080+
for key in reverse_exclude:
1081+
del keymap[key]
10721082
kwtrans = {
10731083
oldkey: kw_.pop(newkey)
10741084
for oldkey, newkey in keymap.items() if newkey in kw_
@@ -2518,6 +2528,15 @@ def get_tick_space(self):
25182528
else:
25192529
return 2**31 - 1
25202530

2531+
def get_tick_params(self, which='major'):
2532+
# docstring inherited
2533+
_api.check_in_list(['major', 'minor'], which=which)
2534+
return self._translate_tick_params(
2535+
getattr(self, f'_{which}_tick_kw'),
2536+
reverse=True,
2537+
reverse_exclude=['left', 'right', 'labelleft', 'labelright'],
2538+
)
2539+
25212540

25222541
class YAxis(Axis):
25232542
__name__ = 'yaxis'
@@ -2759,3 +2778,12 @@ def get_tick_space(self):
27592778
return int(np.floor(length / size))
27602779
else:
27612780
return 2**31 - 1
2781+
2782+
def get_tick_params(self, which='major'):
2783+
# docstring inherited
2784+
_api.check_in_list(['major', 'minor'], which=which)
2785+
return self._translate_tick_params(
2786+
getattr(self, f'_{which}_tick_kw'),
2787+
reverse=True,
2788+
reverse_exclude=['bottom', 'top', 'labelbottom', 'labeltop'],
2789+
)

lib/matplotlib/tests/test_axes.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6777,46 +6777,6 @@ def test_pandas_bar_align_center(pd):
67776777
fig.canvas.draw()
67786778

67796779

6780-
def test_axis_get_tick_params():
6781-
axis = plt.subplot().yaxis
6782-
initial_major_style_translated = {**axis.get_tick_params(which='major')}
6783-
initial_minor_style_translated = {**axis.get_tick_params(which='minor')}
6784-
6785-
translated_major_kw = axis._translate_tick_params(
6786-
axis._major_tick_kw, reverse=True
6787-
)
6788-
translated_minor_kw = axis._translate_tick_params(
6789-
axis._minor_tick_kw, reverse=True
6790-
)
6791-
6792-
assert translated_major_kw == initial_major_style_translated
6793-
assert translated_minor_kw == initial_minor_style_translated
6794-
axis.set_tick_params(labelsize=30, labelcolor='red',
6795-
direction='out', which='both')
6796-
6797-
new_major_style_translated = {**axis.get_tick_params(which='major')}
6798-
new_minor_style_translated = {**axis.get_tick_params(which='minor')}
6799-
new_major_style = axis._translate_tick_params(new_major_style_translated)
6800-
new_minor_style = axis._translate_tick_params(new_minor_style_translated)
6801-
assert initial_major_style_translated != new_major_style_translated
6802-
assert axis._major_tick_kw == new_major_style
6803-
assert initial_minor_style_translated != new_minor_style_translated
6804-
assert axis._minor_tick_kw == new_minor_style
6805-
6806-
6807-
def test_axis_set_tick_params_labelsize_labelcolor():
6808-
# Tests fix for issue 4346
6809-
axis_1 = plt.subplot()
6810-
axis_1.yaxis.set_tick_params(labelsize=30, labelcolor='red',
6811-
direction='out')
6812-
6813-
# Expected values after setting the ticks
6814-
assert axis_1.yaxis.majorTicks[0]._size == 4.0
6815-
assert axis_1.yaxis.majorTicks[0].tick1line.get_color() == 'k'
6816-
assert axis_1.yaxis.majorTicks[0].label1.get_size() == 30.0
6817-
assert axis_1.yaxis.majorTicks[0].label1.get_color() == 'red'
6818-
6819-
68206780
def test_axes_tick_params_gridlines():
68216781
# Now treating grid params like other Tick params
68226782
ax = plt.subplot()

lib/matplotlib/tests/test_axis.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import pytest
23

34
import matplotlib.pyplot as plt
45
from matplotlib.axis import XTick
@@ -8,3 +9,54 @@ def test_tick_labelcolor_array():
89
# Smoke test that we can instantiate a Tick with labelcolor as array.
910
ax = plt.axes()
1011
XTick(ax, 0, labelcolor=np.array([1, 0, 0, 1]))
12+
13+
14+
@pytest.mark.parametrize("which", ["major", "minor"])
15+
@pytest.mark.parametrize(
16+
"axis, expected_basic",
17+
[
18+
(
19+
"x",
20+
{
21+
"bottom": True,
22+
"top": True, # tests use classic.mplstyle with top = True
23+
"labelbottom": True,
24+
"labeltop": False,
25+
"gridOn": False,
26+
},
27+
),
28+
(
29+
"y",
30+
{
31+
"left": True,
32+
"right": True, # tests use classic.mplstyle with right = True
33+
"labelleft": True,
34+
"labelright": False,
35+
"gridOn": False,
36+
},
37+
),
38+
],
39+
ids=["xaxis", "yaxis"],
40+
)
41+
def test_axis_get_tick_params(axis, expected_basic, which):
42+
axis = getattr(plt.subplot(), axis + "axis")
43+
additional_kw = {"labelsize": 42}
44+
45+
axis.set_tick_params(which=which, **additional_kw)
46+
expected = {**expected_basic, **additional_kw}
47+
assert expected == axis.get_tick_params(which=which)
48+
other = "minor" if which == "major" else "major"
49+
assert expected_basic == axis.get_tick_params(which=other)
50+
51+
52+
def test_axis_set_tick_params_labelsize_labelcolor():
53+
# Tests fix for issue 4346
54+
axis_1 = plt.subplot()
55+
axis_1.yaxis.set_tick_params(labelsize=30, labelcolor='red',
56+
direction='out')
57+
58+
# Expected values after setting the ticks
59+
assert axis_1.yaxis.majorTicks[0]._size == 4.0
60+
assert axis_1.yaxis.majorTicks[0].tick1line.get_color() == "k"
61+
assert axis_1.yaxis.majorTicks[0].label1.get_size() == 30.0
62+
assert axis_1.yaxis.majorTicks[0].label1.get_color() == "red"

0 commit comments

Comments
 (0)