1 直方图:效果图和代码如下:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# example data
mu = 100
sigma = 15
x = mu + sigma*np.random.randn(1000)
#直方图的条数
num_bins = 50
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor = "b", alpha=0.5)
#添加一个拟合曲线,返回概率密度函数
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins,y,"r--")
#fontproperties显示中文simHei Lishu STSong Kaiti FangSong YouYuan
plt.xlabel("横轴",fontproperties = "simHei",fontsize = 14)
plt.ylabel("纵轴",fontproperties = "simHei",fontsize = 14)
plt.title("simple histogram")
#text(x,y,str)指定图像位置输入文字;latex语法$$添加公式
plt.text(100,0.033,"$\mu=100$,$\sigma=15$")
#调整图像的间距,防止y轴数值与label重合
plt.subplots_adjust(left=0.15)
plt.show()
主要代码 hist函数:
n, bins, patches = plt.hist(arr,bins=10,normed=0,facecolor="black",edgecolor="black",