8000 Tick label rotation via `set_tick_params` by bcongdon · Pull Request #6829 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Tick label rotation via set_tick_params #6829

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
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
10 changes: 10 additions & 0 deletions doc/users/whats_new/tick_params_rotation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
`Axis.set_tick_params` now responds to 'rotation'
-------------------------------------------------

Bulk setting of tick label rotation is now possible via :func:`set_tick_params` using the `rotation` keyword.

Example
```````
::

ax.xaxis.set_tick_params(which='both', rotation=90)
3 changes: 3 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,9 @@ def tick_params(self, axis='both', **kwargs):
Boolean or ['on' | 'off'], controls whether to draw the
respective tick labels.

*labelrotation*
Tick label rotation.

Example::

ax.tick_params(direction='out', length=6, width=2, colors='r')
Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(self, axes, loc, label,
label1On=True,
label2On=False,
major=True,
labelrotation=0,
):
"""
bbox is the Bound2D bounding box in display coords of the Axes
Expand Down Expand Up @@ -139,6 +140,8 @@ def __init__(self, axes, loc, label,
labelsize = rcParams['%s.labelsize' % name]
self._labelsize = labelsize

self._labelrotation = labelrotation

if zorder is None:
if major:
zorder = mlines.Line2D.zorder + 0.01
Expand Down Expand Up @@ -333,7 +336,7 @@ def _apply_params(self, **kw):
self.tick1line.set_markeredgewidth(v)
self.tick2line.set_markeredgewidth(v)
label_list = [k for k in six.iteritems(kw)
if k[0] in ['labelsize', 'labelcolor']]
if k[0] in ['labelsize', 'labelcolor', 'labelrotation']]
if label_list:
label_kw = dict([(k[5:], v) for (k, v) in label_list])
self.label1.set(**label_kw)
Expand Down Expand Up @@ -798,14 +801,17 @@ def _translate_tick_kw(kw, to_init_kw=True):
'labelsize', 'labelcolor', 'zorder', 'gridOn',
'tick1On', 'tick2On', 'label1On', 'label2On']
kwkeys1 = ['length', 'direction', 'left', 'bottom', 'right', 'top',
'labelleft', 'labelbottom', 'labelright', 'labeltop']
'labelleft', 'labelbottom', 'labelright', 'labeltop',
'rotation']
kwkeys = kwkeys0 + kwkeys1
kwtrans = dict()
if to_init_kw:
if 'length' in kw:
kwtrans['size'] = kw.pop('length')
if 'direction' in kw:
kwtrans['tickdir'] = kw.pop('direction')
if 'rotation' in kw:
kwtrans['labelrotation'] = kw.pop('rotation')
if 'left' in kw:
kwtrans['tick1On'] = _string_to_bool(kw.pop('left'))
if 'bottom' in kw:
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4549,6 +4549,18 @@ def test_bar_color_cycle():
assert ccov(ln.get_color()) == ccov(br.get_facecolor())


@cleanup
def test_tick_param_label_rotation():
fix, ax = plt.subplots()
plt.plot([0, 1], [0, 1])
ax.xaxis.set_tick_params(which='both', rotation=75)
ax.yaxis.set_tick_params(which='both', rotation=90)
for text in ax.get_xticklabels(which='both'):
assert text.get_rotation() == 75
for text in ax.get_yticklabels(which='both'):
assert text.get_rotation() == 90


if 4585 __name__ == '__main__':
import nose
import sys
Expand Down
0