Setting a relative frequency in a matplotlib histogram

39,872

Solution 1

Because normed option of hist returns the density of points, e.g dN/dx

What you need is something like that:

 # assuming that mydata is an numpy array
 ax.hist(mydata, weights=np.zeros_like(mydata) + 1. / mydata.size)
 # this will give you fractions

Solution 2

Or you can use set_major_formatter to adjust the scale of the y-axis, as follows:

from matplotlib import ticker as tick

def adjust_y_axis(x, pos):
    return x / (len(mydata) * 1.0)

ax.yaxis.set_major_formatter(tick.FuncFormatter(adjust_y_axis))

just call adjust_y_axis as above before plt.show().

Solution 3

For relative frequency format set the option density=True. The figure below shows a histogram for 1000 samples taken from a normal distribution with mean 5 and standard deviation 2.0.

Histogram generated with matplotlib

The code is

import numpy as np
import matplotlib.pyplot as plt

# Generate data from normal distibution
mu, sigma = 5, 2.0 # mean and standard deviation
mydata = np.random.normal(mu, sigma, 1000)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(mydata,bins=100,density=True);
plt.show()

If you want % on the y-axis you can use PercentFormatter as shown below

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

# Generate data from normal distibution
mu, sigma = 5, 2.0 # mean and standard deviation
mydata = np.random.normal(mu, sigma, 1000)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(mydata,bins=100,density=False);
ax.yaxis.set_major_formatter(PercentFormatter(xmax=100))
plt.show()

enter image description here

Share:
39,872
user1278140
Author by

user1278140

Updated on July 09, 2022

Comments

  • user1278140
    user1278140 almost 2 years

    I have data as a list of floats and I want to plot it as a histogram. Hist() function does the job perfectly for plotting the absolute histogram. However, I cannot figure out how to represent it in a relative frequency format - I would like to have it as a fraction or ideally as a percentage on the y-axis.

    Here is the code:

    fig = plt.figure()
    ax = fig.add_subplot(111)
    n, bins, patches = ax.hist(mydata, bins=100, normed=1, cumulative=0)
    ax.set_xlabel('Bins', size=20)
    ax.set_ylabel('Frequency', size=20)
    ax.legend
    
    plt.show()
    

    I thought normed=1 argument would do it, but it gives fractions that are too high and sometimes are greater than 1. They also seem to depend on the bin size, as if they are not normalized by the bin size or something. Nevertheless, when I set cumulative=1, it nicely sums up to 1. So, where is the catch? By the way, when I feed the same data into Origin and plot it, it gives me perfectly correct fractions. Thank you!