matplotlib库练习

Ex 11.1

matplotlib库练习_第1张图片

Code

import numpy as np
import matplotlib.pyplot as plt
from pylab import *

x=linspace(0,2,1000)
y=sin(x-2)**2*exp(-x**2)
fig, ax=plt.subplots()
ax.plot(x,y)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Ex 11.1')
plt.show()

Result

matplotlib库练习_第2张图片

Ex 11.2

matplotlib库练习_第3张图片

Code

import numpy as np
import matplotlib.pyplot as plt


X=np.random.rand(20,10)
b=np.random.rand(10)
z=np.random.normal(size=20)
y=X.dot(b)+z
estb=np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
x=np.arange(10)
fig, ax=plt.subplots()
ax.scatter(x,b,color='red',label="true")
ax.scatter(x,estb,color='green',label="estimated")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Ex 11.2')
ax.legend()
plt.show()

Result

matplotlib库练习_第4张图片

Ex 11.3


Code

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from pylab import *

y=np.random.normal(size=10000)
kernel = stats.gaussian_kde(y)
x=np.linspace(-4,4,1000)
plt.hist(y,bins=25,rwidth=0.9,color='b',density=True)
plt.plot(x,kernel.evaluate(x),color='r')
plt.show()

Result

matplotlib库练习_第5张图片

你可能感兴趣的:(matplotlib库练习)