Matplotlib库学习

Learning matplotlib, a lib to draw chart, figure, etc..

Exercise 11.1: Plotting a function

Plot the function
$$ f(x) = sin^2(x-2)e^{-x^2} $$
over the interval [0, 2]. Add proper axis labels, a title, etc.

Code

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt  
import numpy as np

plt.figure(1)
x = np.linspace(0, 2, 100)
y = [ np.sin( x[i] - 2 ) ** 2 * np.exp(- x[i]*x[i]) for i in range(len(x)) ]
plt.plot(x,y)
plt.title('f(x) = sin^2(x-2)*e^(-x^2)')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Result

matplotlib_blog_1

Knowledge

  • pip3 install matplotlib进行安装
  • 遇到问题ModuleNotFoundError: No module named 'tkinter',按教程解决,即用apt install python3-tk安装。
  • plot绘制曲线,xlabelylabel设置坐标轴名称,title设置图像标题

Exercise 11.2: Data

Create a data matrix X with 20 observations of 10 variables. Generate a vector b with parameters. Then generate the response vector $y = Xb+z$ where z is a vector with standard normally distributed variables.

Now (by only using y and X), find an estimator for b, by solving
$$ \hat{b} = arg\min\limits_{b} || Xb-y ||_2 $$
Plot the true parameters b and estimated parameters b̂. See Figure 1 for an example plot.
matplotlib_figure_1

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import matplotlib.pyplot as plt  
import numpy as np

X = np.random.rand(20, 10) * 10 # 增长10倍增大差异
b = np.random.rand(10, 1)
z = np.random.normal(size=(20, 1))

y = X.dot(b) + z
x = np.linspace(0, 9, 10)

b_ = np.array( np.linalg.lstsq(X, y)[0] )

plt.scatter( x, b, c='r', marker='o', label='b')
plt.scatter( x, b_, c='b', marker='+', label='$\hat{b}$')

plt.legend()
plt.show()

Result

matplotlib_blog_2

Knowledge

  • .dot()点乘
  • lstsq()最小二乘法
  • scatter散点图
  • legend显示图例

Exercise 11.3: 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.
matplotlib_figure_2

Code

1
2
3
4
5
6
7
8
9
10
import matplotlib.pyplot as plt  
import numpy as np
from scipy import stats

z = np.random.normal(0,100,size=(10000,))
kernel = stats.gaussian_kde(z)
x = np.linspace(-400, 300, 10000)
plt.hist(z, 25,rwidth=0.8, density=True)
plt.plot(x, kernel.evaluate(x), label='kde')
plt.show()

Result

matplotlib_blog_3

Knowledge

  • hist to draw a histogram
  • scipy.stats.gaussian_kde to create Gaussian kernel density estimator.