|
2 | 2 | =================== |
3 | 3 | Rotated tick labels |
4 | 4 | =================== |
| 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`. |
5 | 10 | """ |
6 | 11 |
|
7 | 12 | import matplotlib.pyplot as plt |
8 | 13 |
|
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 | +} |
12 | 32 |
|
13 | 33 | 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)") |
23 | 37 | plt.show() |
24 | 38 |
|
25 | 39 | # %% |
|
30 | 44 | # in this example: |
31 | 45 | # |
32 | 46 | # - `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