今天就跟大家聊聊有关使用python怎么绘制一个正态分布曲线,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
使用Python绘制正态分布曲线,借助matplotlib绘图工具;
#-*-coding:utf-8-*-
"""
python绘制标准正态分布曲线
"""
# ==============================================================
import numpy as np
import math
import matplotlib.pyplot as plt
def gd(x, mu=0, sigma=1):
"""根据公式,由自变量x计算因变量的值
Argument:
x: array
输入数据(自变量)
mu: float
均值
sigma: float
方差
"""
left = 1 / (np.sqrt(2 * math.pi) * np.sqrt(sigma))
right = np.exp(-(x - mu)**2 / (2 * sigma))
return left * right
if __name__ == '__main__':
# 自变量
x = np.arange(-4, 5, 0.1)
# 因变量(不同均值或方差)
y_1 = gd(x, 0, 0.2)
y_2 = gd(x, 0, 1.0)
y_3 = gd(x, 0, 5.0)
y_4 = gd(x, -2, 0.5)
# 绘图
plt.plot(x, y_1, color='green')
plt.plot(x, y_2, color='blue')
plt.plot(x, y_3, color='yellow')
plt.plot(x, y_4, color='red')
# 设置坐标系
plt.xlim(-5.0, 5.0)
plt.ylim(-0.2, 1)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
plt.legend(labels=['$\mu = 0, \sigma^2=0.2$', '$\mu = 0, \sigma^2=1.0$', '$\mu = 0, \sigma^2=5.0$', '$\mu = -2, \sigma^2=0.5$'])
plt.show()
看完上述内容,你们对使用python怎么绘制一个正态分布曲线有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。