x**x
# x ** x x > 0 # (-x) ** (-x) x < 0 def f(x): y = np.ones_like(x) i = x > 0 y[i] = np.power(x[i], x[i]) i = x < 0 y[i] = np.power(-x[i], -x[i]) return y x = np.linspace(-1.3, 1.3, 101) y = f(x) plt.plot(x, y, 'g-', label='x^x', linewidth=2) plt.grid() plt.legend(loc='upper left') plt.show()
胸型线:
x = np.arange(1, 0, -0.001) y = (-3 * x * np.log(x) + np.exp(-(40 * (x - 1 / np.e)) ** 4) / 25) / 2 plt.figure(figsize=(5,7)) plt.plot(y, x, 'r-', linewidth=2) plt.grid(True) plt.show()
心型线
t = np.linspace(0, 7, 100) x = 16 * np.sin(t) ** 3 y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t) plt.plot(x, y, 'r-', linewidth=2) plt.grid(True) plt.show()
渐开线
t = np.linspace(0, 50, num=1000) x = t*np.sin(t) + np.cos(t) y = np.sin(t) - t*np.cos(t) plt.plot(x, y, 'r-', linewidth=2) plt.grid() plt.show()
Bar
matplotlib.rcParams['font.sans-serif'] = [u'SimHei'] #黑体 FangSong/KaiTi matplotlib.rcParams['axes.unicode_minus'] = False x = np.arange(0, 10, 0.1) y = np.sin(x) plt.bar(x, y, width=0.04, linewidth=0.2) plt.plot(x, y, 'r--', linewidth=2) plt.title(u'Sin曲线') plt.xticks(rotation=-60) plt.xlabel('X') plt.ylabel('Y') plt.grid() plt.show()
均匀分布
x = np.random.rand(10000) t = np.arange(len(x)) plt.hist(x, 30, color='m', alpha=0.5) # plt.plot(t, x, 'r-', label=u'均匀分布') plt.legend(loc='upper left') plt.grid() plt.show()
验证中心极限定理
t = 10000
a = np.zeros(1000)
for i in range(t):
a += np.random.uniform(-5, 5, 1000)
a /= t
plt.hist(a, bins=30, color='g', alpha=0.5, normed=True)
plt.grid()
plt.show()
Poisson分布
x = np.random.poisson(lam=5, size=10000) print x pillar = 15 a = plt.hist(x, bins=pillar, normed=True, range=[0, pillar], color='g', alpha=0.5) plt.grid() # plt.show() print a print a[0].sum()
直方图的使用
mu = 2 sigma = 3 data = mu + sigma * np.random.randn(1000) h = plt.hist(data, 30, normed=1, color='#a0a0ff') x = h[1] y = norm.pdf(x, loc=mu, scale=sigma) #概率密度函数 plt.plot(x, y, 'r--', x, y, 'ro', linewidth=2, markersize=4) plt.grid() plt.show()
差值
rv = poisson(5) x1 = a[1] y1 = rv.pmf(x1) itp = BarycentricInterpolator(x1, y1) # 重心插值 x2 = np.linspace(x.min(), x.max(), 50) y2 = itp(x2) cs = scipy.interpolate.CubicSpline(x1, y1) # 三次样条插值 plt.plot(x2, cs(x2), 'm--', linewidth=5, label='CubicSpine') # 三次样条插值 plt.plot(x2, y2, 'g-', linewidth=3, label='BarycentricInterpolator') # 重心插值 plt.plot(x1, y1, 'r-', linewidth=1, label='Actural Value') # 原始值 plt.legend(loc='upper right') plt.grid() plt.show()