+2 votes
in Programming Languages by (73.8k points)
By default, matplotlib library puts numbers on x-axis ticks. I want to name the ticks on x-axis .e.g Jan, Feb, etc. How can I do that?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use 'set_xticklabels()' to change those tick numbers to your desired values. See the following example.

import numpy as np

import matplotlib.pyplot as plt

from textwrap import wrap

y = [2.6,3.5,1.8,2.4,3.9]

x = np.arange(len(y))

fig, ax = plt.subplots()

title = 'Test Plot'

ax.plot(x,y)

ax.set_xticks(x)

ax.set_xticklabels(['aa','bb','cc','dd','ee'])

plt.title(title, fontsize=13)

plt.show()

 Change numbers on x-axis in matplotlib

by (349k points)
'xticks()' can also be used as follows:

x = ['aa','bb','cc','dd','ee']
plt.xticks(np.arange(len(x)), x, rotation=0)

...