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

Python编程作业【第十一周】(Matplotlib homework)

程序员文章站 2022-06-02 13:55:57
...

Matplotlib Exercise


Exercise 11.1: Plotting a function

import numpy as np
import matplotlib.pyplot as plt
import math
x = np.linspace(-1, 3, 500)
y = np.power(np.sin(x - 2), 2) * np.power(np.e, -1.0 * (np.power(x, 2)))

plt.plot(x, y, 'b-', label = '$\sin^2(x - 2)e^{-x^2}$')
plt.xlim((0, 2))
plt.xlabel('x')
plt.ylabel('y')
plt.title('$\sin^2(x - 2)e^{-x^2}$')
plt.legend()
plt.show()

Python编程作业【第十一周】(Matplotlib homework)

Exercise 11.2: Data

import numpy as np
import matplotlib.pyplot as plt
import math
X = np.random.randint(10, 20, (20, 10))
b = np.random.random(10)
z = np.random.random(20)
y = np.dot(X, b) + z
b_hat = np.linalg.lstsq(X, y, rcond=None)[0]
x = list(range(1, 11))
true_b = plt.scatter(x, b, c='r', marker='x', label='true coefficients')
estimated_b = plt.scatter(x, b_hat, c='b', marker='o', label='estimated coefficients')
plt.legend()
plt.xlabel('index of b')
plt.show()

Python编程作业【第十一周】(Matplotlib homework)

Exercise 11.3: Histogram and density estimation

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import math

x = np.random.normal(loc = 1, scale = 2, size = 1000)
x = sorted(x)
print(x)
plt.hist(x, bins=50, density=True)
kernel = stats.gaussian_kde(x)
plt.plot(x, kernel.pdf(x))
plt.show()

Python编程作业【第十一周】(Matplotlib homework)