From 6578b6498e8455eb681f1881bfa1909b96443ace Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 25 Jun 2024 17:01:32 +0200 Subject: [PATCH] Expand ticklabels_rotation example to cover rotating default ticklabels. set_xticks only works if redefining the ticks at the same time; tick_params works in all cases. --- .../examples/ticks/ticklabels_rotation.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/galleries/examples/ticks/ticklabels_rotation.py b/galleries/examples/ticks/ticklabels_rotation.py index 94924d0440f5..5e21b9a352f0 100644 --- a/galleries/examples/ticks/ticklabels_rotation.py +++ b/galleries/examples/ticks/ticklabels_rotation.py @@ -1,9 +1,7 @@ """ -=========================== -Rotating custom tick labels -=========================== - -Demo of custom tick-labels with user-defined rotation. +==================== +Rotating tick labels +==================== """ import matplotlib.pyplot as plt @@ -14,7 +12,22 @@ fig, ax = plt.subplots() ax.plot(x, y) -# You can specify a rotation for the tick labels in degrees or with keywords. +# 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') plt.show() + +# %% +# +# .. admonition:: References +# +# The use of the following functions, methods, classes and modules is shown +# in this example: +# +# - `matplotlib.axes.Axes.tick_params` / `matplotlib.pyplot.tick_params` +# - `matplotlib.axes.Axes.set_xticks` / `matplotlib.pyplot.xticks`