Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions doc/devel/tag_glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ API tags: what content from the API reference is in the example?
+-----------------------------------+---------------------------------------------+
|``component: annotation`` | |
+-----------------------------------+---------------------------------------------+
|``component: label`` | |
+-----------------------------------+---------------------------------------------+
|``component: ticks`` | |
+-----------------------------------+---------------------------------------------+
|``component: spines`` |frequently paired with ``component: axis`` |
Expand Down
41 changes: 28 additions & 13 deletions galleries/examples/ticks/ticklabels_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@
===================
Rotated tick labels
===================

Rotating tick labels can be useful when the labels are long and overlap with each other.
Adjust the tick properties using `~.Axes.tick_params`: Set the angle in degrees via
the *rotation* parameter. Set the *rotation_mode* parameter to "xtick" / "ytick" to
make the text point towards the tick, see also `~.Text.set_rotation_mode`.
"""

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']
population = {
'India': 1417.492,
'China': 1404.89,
'United States': 341.784857,
'Indonesia': 284.438782,
'Pakistan': 241.499431,
'Nigeria': 223.8,
'Brazil': 213.421037,
'Bangladesh': 169.828911,
'Russia': 146.028325,
'Mexico': 131.001723,
'Japan': 122.95,
'Philippines': 114.1236,
'DR Congo': 112.832,
'Ethiopia': 111.652998,
'Egypt': 107.868296,
'Vietnam': 102.3,
}

fig, ax = plt.subplots()
ax.plot(x, y)
# A tick label rotation can be set using Axes.tick_params.
ax.tick_params("y", rotation=45)
# Alternatively, if setting custom labels with set_xticks/set_yticks, it can
# be set at the same time as the labels.
# For both APIs, the rotation can be an angle in degrees, or one of the strings
# "horizontal" or "vertical".
ax.set_xticks(x, labels, rotation='vertical')

ax.bar(population.keys(), population.values())
ax.tick_params("x", rotation=45, rotation_mode="xtick")
ax.set_ylabel("population (millions)")
plt.show()

# %%
Expand All @@ -30,4 +44,5 @@
# in this example:
#
# - `matplotlib.axes.Axes.tick_params` / `matplotlib.pyplot.tick_params`
# - `matplotlib.axes.Axes.set_xticks` / `matplotlib.pyplot.xticks`
#
# .. tags:: component: ticks, plot-type: bar