Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 721
XåpplI'-I0llwlg'I - Avatar asked Dec 11 '11 07:12

XåpplI'-I0llwlg'I -


1 Answers

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.

like image 145
David Alber Avatar answered Oct 10 '22 20:10

David Alber