How to adjust xlabel position in order to align with major ticks in matplotlib?

Question:

After rotating the x_label, it could not align with major ticks or bars.
as this picture showed

Here is my code:

x = df3[df3['Database'] == 'aminoglycoside'].Resistance_gene
y = df3[df3['Database'] == 'aminoglycoside'].Percent
plt.figure(figsize=(30,15))
plt.xticks(fontsize=20,rotation=45)
plt.margins(0.05)
plt.subplots_adjust(bottom=0.15)
plt.bar(x,y,width=0.5)
Asked By: 崔箐坡

||

Answers:

You can use the horizontalalignment option and align the text to the right, e.g.

import matplotlib.pyplot as plt
plt.plot(range(5))
plt.xticks(range(5),['some','long','labels','even','very long ones'],rotation=45,fontsize=20,horizontalalignment='right')
plt.show()

and get this

and got this

Answered By: X Zhang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.