0

My data consists of the following:

Majority numbers < 60, and then a few outliers that are in the 2000s.

I want to display it in a histogram with the following bin ranges:

0-1, 1-2, 2-3, 3-4, ..., 59-60, 60-max

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as axes

b = list(range(61)) + [2000] # will make [0, 1, ..., 60, 2000]

plt.hist(b, bins=b, edgecolor='black')
plt.xticks(b)

plt.show()

This shows the following:

enter image description here

Essentially what you see is all the numbers 0 .. 60 squished together on the left, and the 2000 on the right. This is not what I want.

So I remove the [2000] and get something like what I am looking for:

enter image description here

As you can see now it is better, but I still have the following problems:

How do I fix this such that the graph doesn't have any white space around (there's a big gap before 0 and after 60).

How do I fix this such that after 60, there is a 2000 tick that shows at the very end, while still keeping roughly the same spacing (not like the first?)

K Split X
  • 3,405
  • 7
  • 24
  • 49

1 Answers1

0

Here is one hacky solution using some random data. I still don't quite understand your second question but I tried to do something based on your wordings

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as axes

fig, ax = plt.subplots(figsize=(12, 6))
data= np.random.normal(10, 5, 5000)

upper = 31
outlier = 2000

data = np.append(data, 100*[upper])
b = list(range(upper)) + [upper]

plt.hist(data, bins=b, edgecolor='black')
plt.xticks(b)
b[-1] = outlier
ax.set_xticklabels(b)
plt.xlim(0, upper)
plt.show()

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71