-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix get_tick_params #27408
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
Fix get_tick_params #27408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Classes derived from Axis must implement get_tick_params | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Tick parameter translation depends on the concrete axis (x or y), so any | ||
axis class derived from `~matplotlib.axis.Axis` must implement | ||
`~matplotlib.axis.Axis.get_tick_params`. This is not necessary if the new axis | ||
class is derived from `.XAxis` or `.YAxis` (as for instance the Axis classes | ||
of `mpl_toolkits.mplot3d`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3394,6 +3394,8 @@ def tick_params(self, axis='both', **kwargs): | |
Width of gridlines in points. | ||
grid_linestyle : str | ||
Any valid `.Line2D` line style spec. | ||
gridOn : bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm hesitant to further expand the use of camelcase variables. But I don't have an overview of its usage and whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As an alternative one could translate If we don't want to have |
||
Whether to draw the grid lines. | ||
|
||
Examples | ||
-------- | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -931,8 +931,7 @@ def set_tick_params(self, which='major', reset=False, **kwargs): | |||||
""" | ||||||
Set appearance parameters for ticks, ticklabels, and gridlines. | ||||||
|
||||||
For documentation of keyword arguments, see | ||||||
:meth:`matplotlib.axes.Axes.tick_params`. | ||||||
For documentation of keyword arguments, see `.Axes.tick_params`. | ||||||
|
||||||
See Also | ||||||
-------- | ||||||
|
@@ -976,6 +975,16 @@ def get_tick_params(self, which='major'): | |||||
""" | ||||||
Get appearance parameters for ticks, ticklabels, and gridlines. | ||||||
|
||||||
.. note:: | ||||||
This method only returns the values of the parameters *bottom*, *top*, | ||||||
*labelbottom*, *labeltop* or *left*, *right*, *labelleft*, *labelright*, | ||||||
respectively, and *girdOn* as well as all additional parameters that were | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
set with `.Axis.set_tick_params` or methods that use it internally, such | ||||||
as `.Axes.tick_params`. The returned parameters may differ from the values | ||||||
of the current elements if they have been set by any other means (e.g. | ||||||
via set_* methods for individual tick objects, `.pyplot.xticks`/ | ||||||
`.pyplot.yticks` or `.rcParams`). | ||||||
|
||||||
.. versionadded:: 3.7 | ||||||
|
||||||
Parameters | ||||||
|
@@ -988,13 +997,6 @@ def get_tick_params(self, which='major'): | |||||
dict | ||||||
Properties for styling tick elements added to the axis. | ||||||
|
||||||
Notes | ||||||
----- | ||||||
This method returns the appearance parameters for styling *new* | ||||||
elements added to this axis and may be different from the values | ||||||
on current elements if they were modified directly by the user | ||||||
(e.g., via ``set_*`` methods on individual tick objects). | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
:: | ||||||
|
@@ -1019,15 +1021,10 @@ def get_tick_params(self, which='major'): | |||||
|
||||||
|
||||||
""" | ||||||
_api.check_in_list(['major', 'minor'], which=which) | ||||||
if which == 'major': | ||||||
return self._translate_tick_params( | ||||||
self._major_tick_kw, reverse=True | ||||||
) | ||||||
return self._translate_tick_params(self._minor_tick_kw, reverse=True) | ||||||
raise NotImplementedError('Derived must override') | ||||||
|
||||||
@staticmethod | ||||||
def _translate_tick_params(kw, reverse=False): | ||||||
def _translate_tick_params(kw, reverse=False, reverse_exclude=[]): | ||||||
""" | ||||||
Translate the kwargs supported by `.Axis.set_tick_params` to kwargs | ||||||
supported by `.Tick._apply_params`. | ||||||
|
@@ -1036,11 +1033,22 @@ def _translate_tick_params(kw, reverse=False): | |||||
to the generic tick1, tick2 logic of the axis. Additionally, there | ||||||
are some other name translations. | ||||||
|
||||||
Returns a new dict of translated kwargs. | ||||||
Parameters | ||||||
---------- | ||||||
kw | ||||||
kwargs dict to translate. | ||||||
reverse | ||||||
whether to translate from set_tick_params kwargs to | ||||||
_apply_params kwargs or back. | ||||||
reverse_exclude | ||||||
list of keys to be removed from the keymap before reverse | ||||||
translating. This is necessary because there are multiple keys | ||||||
with the same value in keymap, depending on the axis. | ||||||
|
||||||
Note: Use reverse=True to translate from those supported by | ||||||
`.Tick._apply_params` back to those supported by | ||||||
`.Axis.set_tick_params`. | ||||||
Returns | ||||||
------- | ||||||
dict | ||||||
new dict of translated kwargs | ||||||
""" | ||||||
kw_ = {**kw} | ||||||
|
||||||
|
@@ -1069,6 +1077,8 @@ def _translate_tick_params(kw, reverse=False): | |||||
'labeltop': 'label2On', | ||||||
} | ||||||
if reverse: | ||||||
for key in reverse_exclude: | ||||||
del keymap[key] | ||||||
kwtrans = { | ||||||
oldkey: kw_.pop(newkey) | ||||||
for oldkey, newkey in keymap.items() if newkey in kw_ | ||||||
|
@@ -2518,6 +2528,15 @@ def get_tick_space(self): | |||||
else: | ||||||
return 2**31 - 1 | ||||||
|
||||||
def get_tick_params(self, which='major'): | ||||||
# docstring inherited | ||||||
_api.check_in_list(['major', 'minor'], which=which) | ||||||
return self._translate_tick_params( | ||||||
getattr(self, f'_{which}_tick_kw'), | ||||||
reverse=True, | ||||||
reverse_exclude=['left', 'right', 'labelleft', 'labelright'], | ||||||
) | ||||||
|
||||||
|
||||||
class YAxis(Axis): | ||||||
__name__ = 'yaxis' | ||||||
|
@@ -2759,3 +2778,12 @@ def get_tick_space(self): | |||||
return int(np.floor(length / size)) | ||||||
else: | ||||||
return 2**31 - 1 | ||||||
|
||||||
def get_tick_params(self, which='major'): | ||||||
# docstring inherited | ||||||
_api.check_in_list(['major', 'minor'], which=which) | ||||||
return self._translate_tick_params( | ||||||
getattr(self, f'_{which}_tick_kw'), | ||||||
reverse=True, | ||||||
reverse_exclude=['bottom', 'top', 'labelbottom', 'labeltop'], | ||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be sufficient to implement the translation, either as a dict or as
_translate_tick_params()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but
_translate_tick_params
is 18 LOC (not counting continuation lines) whereasget_tick_params
is just 2.Another thing is that set/get_tick_params currently doesn't work correctly for Axes3D (I'm going to open an issue about it) and I assume that it's easier fix with dedicated setters/getters (didn't look into it closely yet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, I think you're right and it's less convoluted to make the translation dicts axis-specific in the first place rather than modify a generic dict. I'll change it later this week, changing this PR to draft in the meantime.