Skip to content

Commit bd78ef8

Browse files
committed
DOC: Rewrite tickabel rotation example to use rotation_mode
Closes matplotlib#31221.
1 parent 1017fe1 commit bd78ef8

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

galleries/examples/ticks/ticklabels_rotation.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@
22
===================
33
Rotated tick labels
44
===================
5+
6+
Rotating tick labels can be useful when the labels are long and overlap with each other.
7+
Adjust the tick properties using `~.Axes.tick_params`: Set the angle in degrees via
8+
the *rotation* parameter. Set the *rotation_mode* parameter to "xtick" / "ytick" to
9+
make the text point towards the tick, see also `~.Text.set_rotation_mode`.
510
"""
611

712
import matplotlib.pyplot as plt
813

9-
x = [1, 2, 3, 4]
10-
y = [1, 4, 9, 6]
11-
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']
14+
population = {
15+
'India': 1417.492,
16+
'China': 1404.89,
17+
'United States': 341.784857,
18+
'Indonesia': 284.438782,
19+
'Pakistan': 241.499431,
20+
'Nigeria': 223.8,
21+
'Brazil': 213.421037,
22+
'Bangladesh': 169.828911,
23+
'Russia': 146.028325,
24+
'Mexico': 131.001723,
25+
'Japan': 122.95,
26+
'Philippines': 114.1236,
27+
'DR Congo': 112.832,
28+
'Ethiopia': 111.652998,
29+
'Egypt': 107.868296,
30+
'Vietnam': 102.3,
31+
}
1232

1333
fig, ax = plt.subplots()
14-
ax.plot(x, y)
15-
# A tick label rotation can be set using Axes.tick_params.
16-
ax.tick_params("y", rotation=45)
17-
# Alternatively, if setting custom labels with set_xticks/set_yticks, it can
18-
# be set at the same time as the labels.
19-
# For both APIs, the rotation can be an angle in degrees, or one of the strings
20-
# "horizontal" or "vertical".
21-
ax.set_xticks(x, labels, rotation='vertical')
22-
34+
ax.bar(population.keys(), population.values())
35+
ax.tick_params("x", rotation=45, rotation_mode="xtick")
36+
ax.set_ylabel("population (millions)")
2337
plt.show()
2438

2539
# %%
@@ -30,4 +44,5 @@
3044
# in this example:
3145
#
3246
# - `matplotlib.axes.Axes.tick_params` / `matplotlib.pyplot.tick_params`
33-
# - `matplotlib.axes.Axes.set_xticks` / `matplotlib.pyplot.xticks`
47+
#
48+
# .. tags:: component: ticks

0 commit comments

Comments
 (0)