Set units to X-axis in matplotlib

matplotlib, python

Solution

You need to rotate the text of your ticks, try this:

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker    

plt.plot(range(1000,11000,1000),range(10))

plt.gca().xaxis.set_major_formatter(mticker.FormatStrFormatter('%d cm'))
plt.gca().yaxis.set_major_formatter(mticker.FormatStrFormatter('%d cm'))

for txt in plt.gca().xaxis.get_majorticklabels():
    txt.set_rotation(90)

plt.tight_layout()
plt.show()

Problem

I use this code for add units to axes, but x-axis looks horrible, numbers and units are overwritten. DO you have any idea how to solve it? ``` plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d cm')) plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d cm')) ```

Original source