【2.2】matplotlib-hist(柱形图)

from numpy.random import normal
gaussian_numbers = normal(size=1000)
import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

png

#因为histogram默认是生成10个bin,有时候我们觉得区分细度不够的时候,可以人为来增加bin
plt.hist(gaussian_numbers, bins=20)
plt.show()

png

#我们也可以将纵坐标的频数转换成频率
plt.hist(gaussian_numbers, bins=20, normed=True)
plt.show()

png

#作累积概率分布图(cumulative distribution)
plt.hist(gaussian_numbers, bins=20, normed=True, cumulative=True)
plt.show()

png

#指定bin的横坐标范围
plt.hist(gaussian_numbers, bins=(-10,-1,1,10))
plt.show()

png

#未填充的bar
plt.hist(gaussian_numbers, bins=20, histtype='step')
plt.show()

png

二、更高级的用法

2.1 两个bar图的重叠

import matplotlib.pyplot as plt
from numpy.random import normal, uniform
gaussian_numbers = normal(size=1000)
uniform_numbers = uniform(low=-3, high=3, size=1000)
plt.hist(gaussian_numbers, bins=20, histtype='stepfilled', normed=True, color='b', label='Gaussian')
plt.hist(uniform_numbers, bins=20, histtype='stepfilled', normed=True, color='r', alpha=0.5, label='Uniform')
plt.title("Gaussian/Uniform Histogram")
plt.xlabel("Value")
plt.ylabel("Probability")
plt.legend()
plt.show()

png

参考资料:

https://bespokeblog.wordpress.com/2011/07/11/basic-data-plotting-with-matplotlib-part-3-histograms/

个人公众号,比较懒,很少更新,可以在上面提问题:

更多精彩,请移步公众号阅读:

Sam avatar
About Sam
专注生物信息 专注转化医学