import numpy as np
import itertools
import matplotlib.pyplot as plt
 
 
# 绘制混淆矩阵
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    Input
    - cm : 计算出的混淆矩阵的值
    - classes : 混淆矩阵中每一行每一列对应的列
    - normalize : True:显示百分比, False:显示个数
    """
    if normalize:
        matrix = cm
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        # print("Normalized confusion matrix")
        print("VGG-16")
    else:
        print('Confusion matrix, without normalization')
    plt.figure()
    # 设置输出的图片大小
    figsize = 8, 6
    figure, ax = plt.subplots(figsize=figsize)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    # 设置title的大小以及title的字体
    font_title= {'family': 'Times New Roman',
                  'weight': 'normal',
                  'size': 20,
                  }
    plt.title(title,fontdict=font_title)
    # plt.colorbar()

    cbar = plt.colorbar()
    cbar.ax.tick_params(labelsize=20)
    cbar.ax.yaxis.set_tick_params(width=0)

    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45,)
    plt.yticks(tick_marks, classes)
    # 设置坐标刻度值的大小以及刻度值的字体
    plt.tick_params(labelsize=20)
    labels = ax.get_xticklabels() + ax.get_yticklabels()
    print (labels)
    [label.set_fontname('Times New Roman') for label in labels]
    if normalize:
        fm_int = 'd'
        fm_float = '.2%'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j, i, format(cm[i, j], fm_float),
                     horizontalalignment="center", verticalalignment='bottom',family = "Times New Roman", weight = "normal",size = 20,
                     color="white" if cm[i, j] > thresh else "black")
    else:
        fm_int = 'd'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j, i, format(cm[i, j], fm_int),
                     horizontalalignment="center", verticalalignment='bottom',
                     color="white" if cm[i, j] > thresh else "black")
 
    plt.tight_layout()
    # plt.savefig('confusion_matrix.png', dpi=600, format='png')
 
# cnf_matrix = np.array([[248,16,36],  
#                        [27,253,20],  #     MobileVIT   716
#                        [60,25,215]
# ]

# cnf_matrix = np.array([[233,17,50],  
#                        [18,260,22],  #     EfficientNet    728
#                        [41,24,235]
# ]

# cnf_matrix = np.array([[265,9,26],  
#                        [34,230,36],  #     ConvNeXt    746
#                        [13,36,251]
# ]
# )

# cnf_matrix = np.array([[247,28,25],  
#                        [24,250,26],  #     SK-MNV4    763
#                        [16,18,266]
# ]
# )

#------------------------------------------------------------------------------------------

# cnf_matrix = np.array([[292,6,2],  
#                        [5,289,6],  #        958 ConvNeXt
#                        [5,14,281]
# ]

cnf_matrix = np.array([[290,8,2],  
                       [5,288,7],  #        958 界面
                       [5,14,281]
]

# cnf_matrix = np.array([[294,5,1],  
#                        [1,295,4],  #         973 MIF-SK-MNV4
#                        [8,5,287]
# ]

# cnf_matrix = np.array([[281,9,10],  
#                        [10,287,3],  #         923 EfficientNet
#                        [28,9,263]
# ]
# )

# cnf_matrix = np.array([[261,19,20],  
#                        [4,291,5],  #         921 MobileVIT
#                        [8,15,277]
# ]
)
#------------------------------------------------------------------------------------------

attack_types = ['NM', 'OM', 'LM']
plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=True, title='')
# plot_confusion_matrix(cnf_matrix, classes=attack_types, normalize=True, title='MobileVIT')


# plt.savefig('122.png', dpi=600, format='png')


plt.savefig('confusion_matrix_MIF-SK-MNV4.png', dpi=600, format='png', bbox_inches='tight')
plt.show()

