先说结论:matplotlib 3.1.1 有bug!!!!!!!请更新到新版本。。。。
查看matplotlib当前是哪个版本:
pip list |grep matplotlib
更新:
pip install matplotlib==3.3.4 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
import seaborn as sns
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
y_true = [0,0,1,2,1,2,0,2,2,0,1,1]
y_pred = [1,0,1,2,1,0,0,2,2,0,1,1]
C2= confusion_matrix(y_true, y_pred, labels=[0, 1, 2], normalize='true')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
p1 = sns.heatmap(C2,annot=True,ax=ax)
ax.set_title('Heat Map')
ax.set_xlabel('')
ax.set_ylabel('')
s1 = p1.get_figure()
s1.savefig('HeatMap.jpg',dpi=300, bbox_inches='tight')
更新完后再次运行: