1

I'm using matplotlib in Python and I made a histogram with bars. Now when the histogram appears only the multiples of 5 appear on the x-axis and the multiples of 1000 appear on the y-axis. For the y-axis it's no problem at all but for the x-axis I want the interval to be 1 instead of 5 because I use 1 bar per hour. How can I achieve this?

Thanks in advance.

def plotHistogramTickets():

    hours_NumbersSold = dict()
    for i in range(24):
        hours_NumbersSold[i]=0
    soldTickets = db.getSoldTickets()
    for ticket in soldTickets:
        hourSold = ticket.timeBought.hour
        hours_NumbersSold[hourSold]+=1
    for k,v in hours_NumbersSold.iteritems():
        plt.bar(k,v,1,0)
    plt.xlabel("Hours")
    plt.ylabel("Numbers of tickets sold")
    plt.title("Numbers of tickets sold per hour")
    plt.grid(True)
    plt.show()

1 Answer 1

3

Use xticks(). The following should do the trick for you (put it before the show() command):

plt.xticks(range(25))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! But I have the problem the x-values are till 25, I need 24 instead. Any idea?
That's strange, but if so, just replace 25 with 24: plt.xticks(range(24))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.