[Course] Advanced Computer Programming, Homework, week 12, Matplotlib
程序员文章站
2022-06-02 13:55:45
...
在jupyter notebook中书写,%matplotlib inline
可使图片直接显示
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
1.11 Plotting a function
Plot the function over the interval . 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>
上一篇: LLILC详解,基于LLVM的.NET Core编译器
下一篇: flash as3.0实用公式