欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[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 f(x)=sin2(x2)ex2 over the interval [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}$')

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

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>

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

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>

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

相关标签: Python