直方图尖峰python_Python直方图(histogram)学习笔记-《Python数据科学手册》

例程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');

输出如下:

f451c3190675

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);

得出的图为:

f451c3190675#(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)

得出结果为:

f451c3190675

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值