首页 > 解决方案 > 如何更改 sklearn.metrics 包中的 plot_confusion_matrix 默认图形大小

问题描述

我尝试使用 sklearn.metrics.plot_confusion_matrix 包用 Jupyter notebook 绘制混淆矩阵,但默认图形大小有点小。我在绘图之前添加了 plt.figure(figsize=(20, 20)) ,但是图形大小没有随着输出文本“图形大小 1440x1440 与 0 轴”而改变。如何更改图形大小?

%matplotlib inline
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import plot_confusion_matrix
from matplotlib import pyplot as plt

plt.figure(figsize=(20, 20))
clf = GradientBoostingClassifier(random_state=42)
clf.fit(X_train, y_train)
plot_confusion_matrix(clf, X_test, y_test, cmap=plt.cm.Blues)
plt.title('Confusion matrix')
plt.show()

就像这张图片

标签: python-3.xmatplotlibscikit-learnjupyter-notebookconfusion-matrix

解决方案


我不知道为什么 BigBen 将其作为评论而不是答案发布,但我几乎错过了看到它。这是一个答案,所以未来的围观者不会犯我几乎犯的同样错误!

fig, ax = plt.subplots(figsize=(10, 10))
plot_confusion_matrix(your_model, X_test, y_test, ax=ax)

推荐阅读