在python中画正态分布/正弦曲线图像/心形线

1 在python中画正态分布图像

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

def demo2():
    mu, sigma , num_bins = 0, 1, 50
    x = mu + sigma * np.random.randn(1000000)
    n, bins, patches = plt.hist(x, num_bins, normed=True, facecolor = 'blue', alpha = 0.5)
    y = mlab.normpdf(bins, mu, sigma)
    plt.plot(bins, y, 'r--')
    plt.xlabel('Expectation')
    plt.ylabel('Probability')
    plt.title('histogram of normal distribution: $\mu = 0$, $\sigma=1$')

    plt.subplots_adjust(left = 0.15)
    plt.show()

print('curve')
demo2()

Python使用numpy产生正态分布随机数的向量或矩阵操作示例

#-*- coding:utf-8 -*-
# Python实现正态分布
# 绘制正态分布概率密度函数
import numpy as np
import matplotlib.pyplot as plt
import math
u = 0  # 均值μ
u01 = -2
sig = math.sqrt(0.2) # 标准差δ
x = np.linspace(u - 3*sig, u + 3*sig, 50)
y_sig = np.exp(-(x - u) ** 2 /(2* sig **2))/(math.sqrt(2*math.pi)*sig)
print x
print "="*20
print y_sig
plt.plot(x, y_sig, "r-", linewidth=2)
plt.grid(True)
plt.show()

3 绘制正弦曲线

import math
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 2*math.pi, 0.001)
y = np.sin(x)

plt.plot(x, y)
plt.show()

python代码的爱心曲线

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/84640940
今日推荐