import matplotlib
import matplotlib.pyplot as plt
# sphinx_gallery_thumbnail_number = 2
import pandas as pd
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
d="EMD" ##i 可选项IQ、EMD、VMD
f=8 ##j 可选项4、6、8
k=0.5 ##k 可选项1、0.5、0.1
datapath="./data/{}_test/sample{}/resnet{}/".format(d,k,f)
datasave="./hunxiao_pdf/{}/sample{}/".format(d,k)
data=pd.read_csv(datapath+"label.csv")
pre=data['识别标签']
true=data['原始标签']
pre=np.array(pre)
true=np.array(true)
#生成12个计数变量a0~到a11
a=np.zeros((12,12),dtype=int)
##从csv文件中获得混淆矩阵数据
for i in range(26):
start=int(i*13000)
end=int((i+1)*13000)
pre1=pre[start:end]
true1=true[start:end]
for j in range(len(pre1)):
for z in range(12):
if pre1[j]==z:
a[i][z]=int(a[i][z])+1
print(a)
a=np.array(a)
plt.rc('font',family='Times New Roman')
# 热度图,后面是指定的颜色块,可设置其他的不同颜色
font = {'family': 'Times New Roman',
'weight': 'normal',
'size': 8,
}
font1 = {'family': 'Times New Roman',
'weight': 'normal',
'size': 10,
}
###设置每个子图大小
fig, ax = plt.subplots(figsize=(7,7))
# plt.figure(figsize=(5, 5))
##画布颜色
# plt.imshow(a,cmap=plt.cm.Blues)
im = ax.imshow(a,cmap=plt.cm.Blues,)
##设置坐标轴刻度
classes=['BPSK','QPSK','8PSK','OQPSK','2FSK','4FSK','8FSK','16QAM','32QAM','64QAM','4PAM','8PAM']
# We want to show all ticks...
###设置x,y轴长度
ax.set_xticks(np.arange(len(classes)))
ax.set_yticks(np.arange(len(classes)))
# ... and label them with the respective list entries
ax.set_xticklabels(classes,fontdict=font)
ax.set_yticklabels(classes,fontdict=font)
plt.xlabel('Predicted label', font1)
plt.ylabel('True lable', font1)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(classes)):
for j in range(len(classes)):
text = ax.text(j, i, a[i, j],
ha="center", va="center", color="brown",)
# ax.set_title("data of local classes (in tons/year)")
fig.tight_layout()
plt.savefig(datasave+'resnet{}.pdf'.format(f), bbox_inches='tight')
plt.show()
|