Skip to content

Commit d308eec

Browse files
authored
Merge pull request #6829 from bcongdon/feature_improved_tick_label_rotation
ENH: Tick label rotation via `set_tick_params`
2 parents 80a3f3e + edbe0cc commit d308eec

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
`Axis.set_tick_params` now responds to 'rotation'
2+
-------------------------------------------------
3+
4+
Bulk setting of tick label rotation is now possible via :func:`set_tick_params` using the `rotation` keyword.
5+
6+
Example
7+
```````
8+
::
9+
10+
ax.xaxis.set_tick_params(which='both', rotation=90)

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,9 @@ def tick_params(self, axis='both', **kwargs):
26722672
Boolean or ['on' | 'off'], controls whether to draw the
26732673
respective tick labels.
26742674
2675+
*labelrotation*
2676+
Tick label rotation.
2677+
26752678
Example::
26762679
26772680
ax.tick_params(direction='out', length=6, width=2, colors='r')

lib/matplotlib/axis.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __init__(self, axes, loc, label,
8181
label1On=True,
8282
label2On=False,
8383
major=True,
84+
labelrotation=0,
8485
):
8586
"""
8687
bbox is the Bound2D bounding box in display coords of the Axes
@@ -139,6 +140,8 @@ def __init__(self, axes, loc, label,
139140
labelsize = rcParams['%s.labelsize' % name]
140141
self._labelsize = labelsize
141142

143+
self._labelrotation = labelrotation
144+
142145
if zorder is None:
143146
if major:
144147
zorder = mlines.Line2D.zorder + 0.01
@@ -333,7 +336,7 @@ def _apply_params(self, **kw):
333336
self.tick1line.set_markeredgewidth(v)
334337
self.tick2line.set_markeredgewidth(v)
335338
label_list = [k for k in six.iteritems(kw)
336-
if k[0] in ['labelsize', 'labelcolor']]
339+
if k[0] in ['labelsize', 'labelcolor', 'labelrotation']]
337340
if label_list:
338341
label_kw = dict([(k[5:], v) for (k, v) in label_list])
339342
self.label1.set(**label_kw)
@@ -798,14 +801,17 @@ def _translate_tick_kw(kw, to_init_kw=True):
798801
'labelsize', 'labelcolor', 'zorder', 'gridOn',
799802
'tick1On', 'tick2On', 'label1On', 'label2On']
800803
kwkeys1 = ['length', 'direction', 'left', 'bottom', 'right', 'top',
801-
'labelleft', 'labelbottom', 'labelright', 'labeltop']
804+
'labelleft', 'labelbottom', 'labelright', 'labeltop',
805+
'rotation']
802806
kwkeys = kwkeys0 + kwkeys1
803807
kwtrans = dict()
804808
if to_init_kw:
805809
if 'length' in kw:
806810
kwtrans['size'] = kw.pop('length')
807811
if 'direction' in kw:
808812
kwtrans['tickdir'] = kw.pop('direction')
813+
if 'rotation' in kw:
814+
kwtrans['labelrotation'] = kw.pop('rotation')
809815
if 'left' in kw:
810816
kwtrans['tick1On'] = _string_to_bool(kw.pop('left'))
811817
if 'bottom' in kw:

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,6 +4586,18 @@ def test_bar_color_cycle():
45864586
assert ccov(ln.get_color()) == ccov(br.get_facecolor())
45874587

45884588

4589+
@cleanup
4590+
def test_tick_param_label_rotation():
4591+
fix, ax = plt.subplots()
4592+
plt.plot([0, 1], [0, 1])
4593+
ax.xaxis.set_tick_params(which='both', rotation=75)
4594+
ax.yaxis.set_tick_params(which='both', rotation=90)
4595+
for text in ax.get_xticklabels(which='both'):
4596+
assert text.get_rotation() == 75
4597+
for text in ax.get_yticklabels(which='both'):
4598+
assert text.get_rotation() == 90
4599+
4600+
45894601
if __name__ == '__main__':
45904602
import nose
45914603
import sys

0 commit comments

Comments
 (0)