利用python绘制正态分布曲线

作者:chenzhen0530 时间:2021-04-09 06:59:40 

使用Python绘制正态分布曲线,借助matplotlib绘图工具;

利用python绘制正态分布曲线


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

来源:https://www.cnblogs.com/chenzhen0530/p/10690653.html

标签:python,正态分布,绘制,曲线
0
投稿

猜你喜欢

  • PHP获取当前相对于域名目录的方法

    2023-08-19 18:47:31
  • MySQL与PHP的基础与应用专题之数据查询

    2023-11-10 10:09:55
  • CMS不要让MySQL为你流泪

    2008-12-11 14:38:00
  • Python之csv文件从MySQL数据库导入导出的方法

    2023-08-09 04:45:10
  • 通过MySQL内置全文检索实现中文的相关检索

    2010-06-11 13:20:00
  • centos 安装mysql中遇到问题的解决办法

    2010-12-14 15:11:00
  • Python Collatz序列实现过程解析

    2023-01-11 18:26:23
  • tensorflow使用神经网络实现mnist分类

    2023-07-05 10:19:13
  • 利用Google Ajax Library API加速常用js类库的载入

    2008-06-17 17:44:00
  • ASP用户登录模块的设计源码

    2008-10-03 12:16:00
  • 请给PNG8一个机会

    2009-09-16 14:22:00
  • asp如何正确理解和使用Command、Connection和 Recordset三个对象?

    2010-06-28 18:23:00
  • Pycharm 2020最新永久激活码(附最新激活码和插件)

    2023-06-14 14:21:21
  • Gradio机器学习模型快速部署工具应用分享前篇

    2023-07-23 12:10:45
  • Python序列对象与String类型内置方法详解

    2023-09-22 13:25:18
  • Python3.x+pyqtgraph实现数据可视化教程

    2023-09-25 23:24:47
  • 服务器端控件是如何操作的?

    2009-11-01 15:22:00
  • HTTP中header头部信息详解

    2023-06-11 23:33:17
  • Sql server 2005 找出子表树

    2008-11-24 15:23:00
  • Python程序慢的重要原因

    2023-10-08 16:17:40
  • asp之家 网络编程 m.aspxhome.com