[Course] Advanced Computer Programming, Homework, week 12, Matplotlib

在jupyter notebook中书写,%matplotlib inline 可使图片直接显示

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

1.11 Plotting a function

Plot the function f(x)=sin2(x2)ex2 f ( x ) = s i n 2 ( x − 2 ) e − x 2 over the interval [0,2] [ 0 , 2 ] . Add proper axis labels, a title, etc.

x = np.linspace(0,2,1000)
def f(x):
    return (np.sin(x-2)**2)*np.exp(-x**2)

y = f(x)
plt.plot(x,y)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.title('$f(x)=sin^2(x−2)e^{−x^2}$')
Text(0.5,1,'$f(x)=sin^2(x−2)e^{−x^2}$')

11.2 Data

使用最小二乘法,画出系数矩阵的对比图像

X = np.random.rand(20,10)  
b = np.random.rand(10)  
z = np.random.normal(size=20)  
y = X.dot(b)+z
b0 = np.linalg.inv((X.T).dot(X)).dot((X.T).dot(y)) 
# print(b0)
x = np.linspace(0,9,10)  
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,b,c = 'r',marker = 'o')  
ax.scatter(x,b0,c = 'g',marker = 'x')  
ax.set_title('11.2')  
plt.xlabel('x')  
plt.ylabel('y')  
plt.legend('TE')  
<matplotlib.legend.Legend at 0x2232e112550>

11.3 Histogram and density estimation

Histogram and density estimation Generate a vector z of 10000 observations from your favorite exotic distribution. Then make a plot that shows a histogram of z (with 25 bins), along with an estimate for the density, using a Gaussian kernel density estimator (see scipy.stats). See Figure 2 for an example plot.
画出z的直方图,使用核密度估计法估计密度并且作图

from scipy.stats import gaussian_kde  

x = np.random.normal(size=1000)  
y = np.random.normal(size=1000)  
xx = np.vstack([x,y])  
z = gaussian_kde(xx)(xx)  
f,(ax1,ax2) = plt.subplots(1,2,figsize=(6,3))  
ax1.hist(x, bins=25, normed=True, color='b')  
sc = ax2.scatter(x,y,c=z)
plt.colorbar(sc)
<matplotlib.colorbar.Colorbar at 0x2233238c898>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值