混淆矩阵字体大小

CRo*_*NiC 3 python confusion-matrix scikit-learn

我有一个带有非常小的数字的混淆矩阵,但我找不到改变它们的方法。

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, rf_predictions)
ax = plt.subplot()
sns.set(font_scale=3.0) #edited as suggested
sns.heatmap(cm, annot=True, ax=ax, cmap="Blues", fmt="g");  # annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');
ax.set_ylabel('Observed labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(['False', 'True']);
ax.yaxis.set_ticklabels(['Flase', 'True']);
plt.show()
Run Code Online (Sandbox Code Playgroud)

这就是我正在使用的代码,我得到的图片如下: 在此处输入图片说明

我不介意手动更改分类的编号,但我真的不想为标签也这样做。

编辑:数字现在更大了,但标签仍然很小

干杯

小智 6

使用 rcParams 更改图中的所有文本:

fig, ax = plt.subplots(figsize=(10,10))
plt.rcParams.update({'font.size': 16})
disp = plot_confusion_matrix(clf, Xt, Yt,
                             display_labels=classes,
                             cmap=plt.cm.Blues,
                             normalize=normalize,
                             ax=ax)
Run Code Online (Sandbox Code Playgroud)


Jaf*_*ado 5

使用sns.set来改变热图值的字体大小。您可以在ax.set_xlabelax.set_ylabel和中将标签和标题的字体大小指定为字典ax.set_title,并使用 指定刻度标签的字体大小ax.tick_params


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, rf_predictions)

ax = plt.subplot()
sns.set(font_scale=3.0) # Adjust to fit
sns.heatmap(cm, annot=True, ax=ax, cmap="Blues", fmt="g");  

# Labels, title and ticks
label_font = {'size':'18'}  # Adjust to fit
ax.set_xlabel('Predicted labels', fontdict=label_font);
ax.set_ylabel('Observed labels', fontdict=label_font);

title_font = {'size':'21'}  # Adjust to fit
ax.set_title('Confusion Matrix', fontdict=title_font);

ax.tick_params(axis='both', which='major', labelsize=10)  # Adjust to fit
ax.xaxis.set_ticklabels(['False', 'True']);
ax.yaxis.set_ticklabels(['False', 'True']);
plt.show()
Run Code Online (Sandbox Code Playgroud)