4
votes

I have a Python histogram.

I want to normalise the peak of the histogram to 1 so that only the relative height of the bars is important.

I see some methods of doing this that involve changing the bin width, but I don't want to do this.

I also realise that I could just change the labels of the y axis, but I also have another plot overlaid, so the yticks must be the actual values.

Is there no way to access and change the histogram "count" in each bin?

Thank you.

1

1 Answers

4
votes

I think what you're after is a normalized form of your histogram, where the y-axis is the density instead of the count. If you're using Numpy, just use the normed flag in the histogram function.

If you want the peak of your histogram to be 1, then you can divide the count in each bin by the maximum bin value, i.e. (building off the SO MatPlotLib example here):

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

# Generate random data
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(10000)

# Create the histogram and normalize the counts to 1
hist, bins = np.histogram(x, bins = 50)
max_val = max(hist)
hist = [ float(n)/max_val for n in hist]

# Plot the resulting histogram
center = (bins[:-1]+bins[1:])/2
width = 0.7*(bins[1]-bins[0])
plt.bar(center, hist, align = 'center', width = width)
plt.show()

enter image description here