How to change ticks on a histogram? (matplotlib)

I have a histogram with only a few values. As a result, the x axis ticks are decimals:

enter image description here

How can I make it 1,2,3,4,5?

Upvotes: 9

Views: 25432

Answers (1)

David Alber
David Alber

Reputation: 18091

You can use matplotlib.pyplot.xticks to set the locations of the x-axis tick marks.

Without the code used to generate the question's histogram, I resorted to creating data to produce a similar histogram. In this first example, we have a histogram with the default tick marks.

from pylab import hist, show

x = [1.1]*29 + [2]*7 + [3.2]*3 + [5]
hist(x)
show()

Histogram with default tick marks.

The objective is to have tick marks at 1, 2, 3, 4, and 5, and the next example does this by using xticks.

from pylab import hist, show, xticks

x = [1.1]*29 + [2]*7 + [3.2]*3 + [5]
hist(x)
xticks(range(1, 6))
show()

Histogram with modified tick marks.

Upvotes: 9

Related Questions