close

How can I rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How can I rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal?

  1. How can I rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal?

    So if rotation_mode is not specified, the text is first rotated and then aligned. In this mode, the bounding box is not exactly the top right corner of the text even if ha="right" is used.

  2. rotate xticklabels in matplotlib so that the spacing between each xticklabel is equal

    So if rotation_mode is not specified, the text is first rotated and then aligned. In this mode, the bounding box is not exactly the top right corner of the text even if ha="right" is used.

Method 1

I looked into the documentation of the matplotlib.text.Text.set_rotation_mode

set_rotation_mode(self, m)

Set text rotation mode.

Parameters: 
m : {None, 'default', 'anchor'}
If None or "default", the text will be first rotated, 
then aligned according to their horizontal and vertical 
alignments. 
If "anchor", then alignment occurs before rotation.

So if rotation_mode is not specified, the text is first rotated and then aligned. In this mode, the bounding box is not exactly the top right corner of the text even if ha="right" is used.

If rotation_mode="anchor", the text is directly rotated about the anchor point (ha="right").

Here is an example

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['G1_bla_bla', 'G2_bla', 'G3_bla', 'G4_bla', 'G5_bla']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars
fig, ax = plt.subplots()
ax.bar(x - width/2, men_means, width, label='Men')
ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(
    labels, 
    rotation=30, 
    ha="right",  
    rotation_mode="anchor")  # <====== HERE is the key
ax.legend()
plt.show()

The plot now has the correct alignment:

enter image description here

Method 2

If the rotation angle is ~45 deg, then Ernest’s ha='right and gbinux’s rotation_mode='anchor' are great:

ax.set_xticklabels(xticklabels, rotation=45, ha='right', rotation_mode='anchor')

However this does not work well for other rotation angles, e.g. 70 deg (see left subplot).

If the rotation angle is not ~45 deg, combine ha='right' instead with a ScaledTranslation (see right subplot).

without and with ScaledTranslation
...
ax.set_xticklabels(xticklabels, rotation=70, ha='right')

# create offset transform (x=5pt)
from matplotlib.transforms import ScaledTranslation
dx, dy = 5, 0
offset = ScaledTranslation(dx/fig.dpi, dy/fig.dpi, scale_trans=fig.dpi_scale_trans)

# apply offset transform to all xticklabels
for label in ax.xaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read