这篇文章主要介绍了在Python中画图(基于Jupyter notebook的魔法函数),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

先展示一段相关的代码:
#we test the accuracy of knn and find the k which makes the biggest accuracy
k_range=list(range(1,26))#[1,25]
scores=[]
for k in k_range:
knn=KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train,y_train)
y_pred=knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test,y_pred))
#------------ prepare the data we need to plot-------------------
#we draw a graph to show the result
import matplotlib.pyplot as plt
#a magic function,which allows polts to appear whitin the notebook
%matplotlib inline
plt.plot(k_range,scores)
plt.xlabel('Value of K for KNN')
plt.ylabel('Testing Accuracy')