简体   繁体   中英

Strange behavior in matplotlib (multiple) histograms

I tried to plot two histograms on the same canvas, using the standard method:

    plt.figure(figsize=(8,6))
    plt.hist(samp_1_I, label=['Female'], alpha=0.5)
    plt.hist(samp_0_I, label=['Male'], alpha=0.5)
    plt.tick_params(axis='both', which='major', labelsize=18)
    plt.legend(fontsize=18)
    plt.show()

and the result is: 在此处输入图片说明 If you notice, histogram 'bars' are not aligned towards the end (right most side). How to fix it? Has anyone seen this before?

The horizontal axis are rational numbers [0,1] and by default it is binned into 10 bin of 0.1. I don't understand why bar of two groups are not aligned as in the beginning.

If you want 10 bins with binwidth 0.1, you need to provide those bins in the call to plt.hist .

import matplotlib.pyplot as plt
import numpy as np

samp_1_I = np.random.rand(14)
samp_0_I = np.random.rand(17)

bins= np.arange(11)/10.

plt.figure(figsize=(8,6))
plt.hist(samp_1_I, bins=bins, label=['Female'], alpha=0.5)
plt.hist(samp_0_I, bins=bins, label=['Male'], alpha=0.5)
plt.tick_params(axis='both', which='major', labelsize=18)
plt.legend(fontsize=18)
plt.show()


Answering the question from the comments: Using normed=True you'll get the normalized frequency in the sense that the sum over each bin width times the the frequency is 1 .

sample = np.random.rand(14)
bins= np.arange(11)/10.
hist, _bins = np.histogram(sample, bins=bins, normed = True)
print hist
print np.sum ( hist * np.diff(bins) ) # This prints 1.0

This holds even if bin widths are different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM