Skip to content

Commit 5f7fc6d

Browse files
committed
Allow rotation of tick labels
1 parent 85d40d0 commit 5f7fc6d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

matplotlib2tikz/axes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,35 @@ def __init__(self, data, obj):
198198
assert direction == 'inout'
199199
self.axis_options.append('tick align=center')
200200

201+
# Set each rotation for every label
202+
x_tick_labels_rotation = \
203+
[label.get_rotation() for label in obj.xaxis.get_majorticklabels()]
204+
if any(x_tick_labels_rotation) != 0:
205+
# check if every value is the same
206+
if len(set(x_tick_labels_rotation)) == 1:
207+
self.axis_options.append('xticklabel style = {rotate=%d}' %
208+
x_tick_labels_rotation[0])
209+
else:
210+
value = 'every x tick label/.style = {\n' \
211+
'rotate={,%s}[\\ticknum]\n' \
212+
'}' % ','.join(str(x) for x in x_tick_labels_rotation)
213+
214+
self.axis_options.append(value)
215+
216+
y_tick_labels_rotation = \
217+
[label.get_rotation() for label in obj.yaxis.get_majorticklabels()]
218+
if any(y_tick_labels_rotation) != 0:
219+
# check if every value is the same
220+
if len(set(y_tick_labels_rotation)) == 1:
221+
self.axis_options.append('yticklabel style = {rotate=%d}' %
222+
y_tick_labels_rotation[0])
223+
else:
224+
value = 'every y tick label/.style = {\n' \
225+
'rotate={,%s}[\\ticknum]\n' \
226+
'}' % ','.join(str(x) for x in y_tick_labels_rotation)
227+
228+
self.axis_options.append(value)
229+
201230
# Don't use get_{x,y}gridlines for gridlines; see discussion on
202231
# <http://sourceforge.net/p/matplotlib/mailman/message/25169234/>
203232
# Coordinate of the lines are entirely meaningless, but styles

test/testfunctions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'noise2',
2626
'patches',
2727
'quadmesh',
28+
'rotated_labels',
2829
'scatter',
2930
'scatter_colormap',
3031
'sharex_and_y',

test/testfunctions/rotated_labels.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
3+
desc = 'Rotated labels'
4+
phash = 'a91fa323753d8d03'
5+
6+
7+
def plot():
8+
from matplotlib import pyplot as plt
9+
10+
x = [1, 2, 3, 4]
11+
y = [1, 4, 9, 6]
12+
13+
fig = plt.figure()
14+
15+
plt.subplot(611)
16+
plt.plot(x, y, 'ro')
17+
plt.xticks(x, rotation='horizontal')
18+
19+
plt.subplot(612)
20+
plt.plot(x, y, 'ro')
21+
plt.xticks(x, rotation='vertical')
22+
23+
ax = plt.subplot(613)
24+
ax.plot(x, y, 'ro')
25+
plt.xticks(y, rotation=-25)
26+
27+
ax = plt.subplot(614)
28+
ax.plot(x, y, 'ro')
29+
plt.yticks(y, rotation=-25)
30+
31+
ax = plt.subplot(615)
32+
ax.plot(x, y, 'ro')
33+
34+
for idx, label in enumerate(ax.get_xticklabels()):
35+
label.set_rotation(45 if idx % 2 == 0 else 0)
36+
37+
ax = plt.subplot(616)
38+
ax.plot(x, y, 'ro')
39+
40+
for idx, label in enumerate(ax.get_yticklabels()):
41+
label.set_rotation(90 if idx % 2 == 0 else 0)
42+
43+
return fig

0 commit comments

Comments
 (0)