Apollo
Apollo

Reputation: 9054

Histogram with uneven heights within bins

Why does my histogram look like this? I'm plotting it using the following code:

import matplotlib.pyplot as plt

n, bins, patches = plt.hist(data, 25)
plt.show()

where data contains about 500,000 sorted points. Shouldn't each bin be smooth at the top?

enter image description here

Upvotes: 0

Views: 46

Answers (2)

Hans
Hans

Reputation: 2615

I suspect your data might not be a flat list, but, e.g., a list of lists or similarly structured. In that case, hist will bin by sublist. This script demonstrates the difference:

import random
from matplotlib import pyplot as plt

data = [[random.randint(0, x) for x in range(20)] for y in range(100)]

plt.subplot(211)
# structured
plt.hist(data, 25)

plt.subplot(212)
# flattened
plt.hist([i for l in data for i in l], 25)

plt.show()

You will probably have to use some kind of similar "flattening" on your data.

Upvotes: 1

Ian
Ian

Reputation: 1716

I think this example from the matplotlib gallery displays the kind of plot you want. Therefore, you should either use histtype="stepfilled" or histtype="bar".

Upvotes: 0

Related Questions