import numpy as np
import matplotlib.pyplot as plt
#matplotlib.pyplot 是绘制各类可视化图形的命令子库
import matplotlib.mlab as mlab
#1)确定样本点
np.random.seed(0)
mu=100#均值
sigma=15#标准差
x=mu+sigma*np.random.randn(457)#标准正太分布与非标准正太分布相互转换 X~N(mu,sigma**2) Y=(X-mu)/sigma~N(0,1) 其中randn函数就是生成标准正太分布的样本数组点
print(x)
fig,axs=plt.subplots(1,2)#subplot_kw=dict(polar=True)polar极地图,类似于极坐标系图
ax1=axs[0]
n,bins,patches=ax1.hist(x=x,bins=50,normed=True,histtype='stepfilled',color=(0.6,0.5,0.3),alpha=0.75)#alpha透明度
'''
频率分布直方图
x轴为样本数据的区间划分
y轴为样本数据在 每个区间 的 频率分布 或者是 个数分布 情况
:param x: 样本数据,是一个一维数组
:param bin: 样本数据 最大值和最小值之间划分的区间的个数
:param nomerd:True-->y轴是出现频率 (落在此区间的个数/总样本点个数) False -->点的个数
:param histtype:{'bar', 'barstacked', 'step', 'stepfilled'}
'''
print("bins:",bins)#每一块bins 的edge值
x.sort()#从小到大排列
print("x:",x)#样本数据
y=mlab.normpdf(bins,mu,sigma)# 将bins数组的带入由mu sigma唯一确定额正太分布函数中,得出y值
ax1.plot(bins,y,'--')#scatter 散点图 plot线图
ax1.axis([0,200,0,1])#axis([x_start,x_end,y_start,y_end])
ax1.set_xlabel('Smarts')
ax1.set_ylabel('Probability density')#density 频率
ax1.set_title('Histogram IQ $\mu=100$, $\sigma=15$')
#fig.tight_layout()
plt.show()
plt.savefig('histogram_test1.png')
linux 下matplotlib 运用二
最新推荐文章于 2024-01-22 21:46:31 发布