来自mglearn的剧情

时间:2019-11-06 17:25:13

标签: python pdf plot

如何从mglearn保存情节?我尝试了这段代码,但是它不起作用。

import numpy as np
import matplotlib.pyplot as plt
import mglearn
from fpdf import FPDF


f = mglearn.plots.plot_knn_classification(n_neighbors=3)
f.savefig("n_neighbors.pdf", bbox_inches='tight')

错误是AttributeError:“ NoneType”对象没有属性“ savefig”。 谢谢

2 个答案:

答案 0 :(得分:0)

由于mglearn软件包使用matplotlib.pyplot的绘图机制,因此您可以使用其保存机制,

mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')

答案 1 :(得分:0)

from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots

import numpy as np
import mglearn
from fpdf import FPDF

X, y = mglearn.datasets.make_forge()


mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.savefig("n_neighbors.pdf", bbox_inches='tight')

此代码有效。