4

I created a figure which shows a set of data and a histogram. What bugs me is, as shown below, the X-axis at the histogram has a step of 20 therefore the last value is 140 instead of 150 and this triggers my OCD very badly. Can someone help me in fixing it?

The png file generated:

https://i.stack.imgur.com/NhBYM.png

And the relevant part of the code:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(60, 13, 500)

plt.hist(data, orientation = 'horizontal')
plt.grid()
plt.axis([0, 150, 0, 120])

plt.savefig('HISTOGRAM.png')

Thank you!

  • Btw I looked up the internet for answers but could not find any useful. Maybe I used the wrong keywords... anyway I hope someone can help me out! –  Mar 15 '19 at 03:05

2 Answers2

3

What you are looking for is plt.xticks():

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(60, 13, 500)

plt.hist(data, orientation = 'horizontal')
plt.grid()
plt.axis([0, 150, 0, 120])
plt.xticks(np.arange(0,151,25))

plt.savefig('HISTOGRAM.png')

There you can specify where to put the ticks. Same for y-axis.

GenError
  • 885
  • 9
  • 25
0

A cure for your OCD

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(60, 13, 500)

plt.hist(data, orientation = 'horizontal')
plt.grid()
plt.xticks(np.linspace(0,150,16))
plt.axis([0, 150, 0, 120])
plt.savefig('HISTOGRAM.png')

basically plt.xticks and plt.yticks accept lists as input and use them as markers on the x axis and y axis respectively, np.linspace generates an array with start,stop and number of points.

Happy Coding

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
  • If I got it right: with `linspace` I can specify the number of ticks and with `arange` I can specify the space between the ticks. In this case, the second method would be more beneficial to me. And now I can go to sleep in peace. Thanks to both of you!!! –  Mar 15 '19 at 03:31
  • @benjibenjibenji yup that is exactly correct, and yes I also think `arange` would be better for you in this case. – anand_v.singh Mar 15 '19 at 03:35