How to use 'matplotlib rotate tick labels' in Python

Every line of 'matplotlib rotate tick labels' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
17def rotateTickLabels(ax, rotation, which, rotation_mode="anchor", ha="right"):
18 """ Rotates the ticklabels of a matplotlib Axes
19
20 Parameters
21 ----------
22 ax : matplotlib Axes
23 The Axes object that will be modified.
24 rotation : float
25 The amount of rotation, in degrees, to be applied to the labels.
26 which : string
27 The axis whose ticklabels will be rotated. Valid values are 'x',
28 'y', or 'both'.
29 rotation_mode : string, optional
30 The rotation point for the ticklabels. Highly recommended to use
31 the default value ('anchor').
32 ha : string
33 The horizontal alignment of the ticks. Again, recommended to use
34 the default ('right').
35
36 Returns
37 -------
38 None
39
40 """
41
42 if which == "both":
43 rotateTickLabels(ax, rotation, "x", rotation_mode=rotation_mode, ha=ha)
44 rotateTickLabels(ax, rotation, "y", rotation_mode=rotation_mode, ha=ha)
45 else:
46 if which == "x":
47 axis = ax.xaxis
48
49 elif which == "y":
50 axis = ax.yaxis
51
52 for t in axis.get_ticklabels():
53 t.set_horizontalalignment(ha)
54 t.set_rotation(rotation)
55 t.set_rotation_mode(rotation_mode)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

270def set_xticklabels(xticklabels,
271 rotation = 'vertical',
272 fontsize = 16,
273 **kargs):
274 ax = plt.gca()
275 ax.set_xticklabels(xticklabels, rotation = rotation,
276 fontsize = fontsize, **kargs)

Related snippets