阅读链接:Python Data Science Handbook | Python Data Science Handbook
例程1:
import numpy as np
import matplotlib.pyplot as plt
# (Example 1) set the style of the gram
plt.style.use('seaborn-white')
#generate a set of 1000 random numbers with a standard Gaussian distribution
data = np.random.randn(1000)
plt.hist(data, bins=20, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='None');
随机数生成可参考:numpy.random.randn()用法_u012149181的博客-CSDN博客
风格样式设置可参考:plt.style.use设置背景样式_qq_22592457的博客-CSDN博客
输出如下:
bins参数为分箱个数(数值越大,分组越多,小矩形越窄),alpha是透明度其值可取[0,1]。
normed参数的官方解释是:如果为True,则返回元组的第一个元素将是归一化以形成概率密度的计数,
跑出来的区别就是,true的波形比false更接近正态分布
例程2:
#括号里第一个和第二个参数分别是正态分布期望及方差,第三个参数表示随机数个数
#(Example 2)括号里第一个和第二个参数分别是正态分布期望及方差,第三个参数表示随机数个数
x1=np.random.normal(0,0.8,1000)
x2=np.random.normal(-2,1,1000)
x3=np.random.normal(3,2,1000)
kwargs=dict(histtype='stepfilled',alpha=0.3,normed=True,bins=40)
plt.hist(x1,**kwargs)
plt.hist(x2,**kwargs)
plt.hist(x3,**kwargs);
得出的图为:
#(Example 3)计算分箱情况和分箱数
data = np.random.randn(1000)
counts, bin_edges = np.histogram(data, bins=5)
plt.hist(data);
#compute the sum of elements of list"count"
def sum_list_ele(list):
total=0
ele = 0
while(ele < len(list)):
total += list[ele]
ele += 1
return total
print(counts)
print(sum_list_ele(counts))
print(bin_edges)
得出结果为: