from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import matplotlib
def plotCM(matrix, savname=None, classes=None):
"""classes: a list of class names"""
if classes==None:
classes = [str(i) for i in range(matrix.shape[0])]
# 设置字体全局Times New Roman :注释后即可显示中文
plt.rc('font', family='Times New Roman')
del matplotlib.font_manager.weight_dict['roman']
matplotlib.font_manager._rebuild()
# 设置字体大小等一些参数
config = {
"font.size": 7,
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
'font.sans-serif': ['SimHei'],
'figure.figsize': (2.25,2.25), # 布局大小为(2.25,2.25)
}
# 参数更新
plt.rcParams.update(config)
# Normalize by row
matrix = matrix.astype(np.float)
linesum = matrix.sum(1)
linesum = np.dot(linesum.reshape(-1, 1), np.ones((1, matrix.shape[1])))
matrix /= linesum
# plot
plt.switch_backend('agg')
fig = plt.figure()
ax = fig.add_subplot(111)
# vmin, vmax:这些参数本质上是可选的,它们是颜色栏范围。
# cmap 颜色条
cax = ax.matshow
混淆矩阵绘制并保存
于 2022-05-30 23:31:40 首次发布
本文介绍了如何在Python中操作坐标轴刻度、设置刻度间隔和标签,以及控制坐标轴显示,适用于数据可视化和矩阵绘制场景。
摘要由CSDN通过智能技术生成