-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matplotlib 绘图相关 #49
Comments
对三维数据可视化有时将三维数据展示在二维平面上很有用,比如绘制热力图、等高线等。 使用 contourf / contourf 来绘制等高线、热力图# 函数 f 根据 x, y 计算出 z
def f(x, y):
return x**2 + y**2
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 20)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.contour(X, Y, Z, 30, cmap=plt.cm.rainbow)
plt.colorbar()
plt.contourf(X, Y, Z, 30, cmap=plt.cm.rainbow)
plt.colorbar() 绘制分类器决策面from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=100, centers=2, cluster_std=2, random_state=42)
plt.scatter(X[:,0], X[:,1], c=y, s=20, cmap=plt.cm.Dark2) from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(X, y)
def plot_predictions(clf, axes):
x1 = np.linspace(axes[0], axes[1], 100)
x2 = np.linspace(axes[2], axes[3], 100)
X1, X2 = np.meshgrid(x1, x2)
X = np.vstack((X1.ravel(), X2.ravel())).T
Y = clf.predict(X).reshape(X1.shape)
plt.contourf(X1, X2, Y, cmap=plt.cm.tab10, alpha=0.2)
plt.scatter(X[:,0], X[:,1], c=y, s=10, cmap=plt.cm.tab10)
plot_predictions(clf, [-10, 10, -10, 15]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: