在使用seaborn绘制热图时,数字出现在热图的边界上。我已经用相同的代码多次绘制过热图。之前一直运行良好,但现在我遇到了错位的问题。
使用的代码
#定义一个自定义函数def get_confusion_matrix(clf,X_te,y_test):y_pred = clf.predict(X_te)df_cm = pd.DataFrame(confusion_matrix(y_test, y_pred), range(2),range(2))df_cm.columns = ['预测为否','预测为是']df_cm = df_cm.rename({0: '实际为否', 1: '实际为是'})sns.set(font_scale=1.4)#用于标签大小sns.heatmap(df_cm, annot=True,annot_kws={"size": 16}, fmt='d',cmap="YlGnBu", linewidths=5)#使用函数绘制热图get_confusion_matrix(model_s1,X_tr,y_train_1)
回答:
当前版本的matplotlib破坏了热图。请将包降级到3.1.0版本
pip install matplotlib==3.1.0
附注:答案从 datascience.stackexchange 复制粘贴而来,因为答案不在StackOverflow上