python matplotlib绘图、混淆矩阵 汉字字体、数字、英文字母的设置

最近在写一篇文章的时候,文章对图片格式有要求:图中汉字用宋体六号、数字和英文字母用新罗马字体,这些都需要在一张图中表现出来 。经过一番查找摸索,现归纳整理如下:
对坐标轴设置
现假设有如下要求:在一张图中的坐标轴用数字来表示刻度,还要用汉字来定义坐标轴含义,而且图中还有英文图例 。
在一张图中(下图1所示),我们用代码需要绘制出同时满足以上要求的图,就需要在程序中进行全局和局部的设置,以满足要求 。
从图中可以看出,这张图满足了我们的要求,这张图是用绘制的 。下面就列出代码,关键点处会加以说明 。
import matplotlib.pyplot as plt #这行代码必不可少,就不多说了#下面以字典的形式将对应的点列出,这是我自己跑了几个模型的准确率和验证准确率的值,总共是50个epoch,部分数据被我删除了(处于保密),你们可以自己随便写一些数值,将其补全mobilenet={ 'acc':[0.5204,0.8537,0.8873,0.9041,0.9281,0.9329,0.9496,0.9472,0.9664,0.9712,0.9736,0.9544,0.9616,0.9640,0.9928,0.9808,0.9640,0.9712,0.9688,0.9784,0.9496,0.9808,0.9688,0.9640,0.9856,0.9664,0.9664,0.9712,0.9964,0.9808,0.9736,0.9784,0.9736,0.9880,0.9808,0.9928,0.9880,0.9880,0.9808,0.9928,0.9976,0.9952,0.9856,0.9856,0.9952,0.9808,0.9856,0.9904,0.9928,0.9836],'val_acc':[0.3741,0.3741,0.4388,0.5504,0.5252,0.4892,0.6338,0.5180,0.4676,0.4964,0.6475,0.7122,0.6619,0.6691,0.6547,0.6331,0.6835,0.7291,0.8417,0.7410,0.7770,0.7163,0.6901,0.7338,0.7842,0.7986,0.7842,0.7626,0.8345,0.9137,0.9256,0.9065,0.8777,0.8921,0.8058,0.7626,0.7852,0.8129,0.8561,0.9137,0.9137,0.9065,0.9353,0.9496,0.9353,0.9640,0.9568,0.9424,0.9237,0.9320]}xception={'acc': [],'val_acc':[]NASNetMobile={'acc': [],'val_acc':[]desnet121={'acc': [],'val_acc':[]VGG19={'acc': [],'val_acc':[]resnet101={'acc': [],'val_acc':[]#字典创建好以后就取出字典的值,将其付给一个变量(这部分对吗应给能看懂吧,要是看不懂,就要好好补一下基础知识了)res50_acc=resnet101['acc']res50_val_acc=resnet101['val_acc']vgg19_acc=VGG19['acc']vgg19_val_acc=VGG19['val_acc']des121_acc=desnet121['acc']des121_val_acc=desnet121['val_acc']#接下来就开始绘图了epoch = [i for i in range(50)]plt.plot(epoch,res50_acc, 'r.-',linewidth=1.2, label='ResNet101_acc')plt.plot(epoch, res50_val_acc, 'r-',linewidth=1.2, label='ResNet101_val_acc')#在plt.plot()函数里面设置了坐标轴变量、曲线类型、线宽、曲线标签等plt.plot(epoch, vgg19_acc, 'g.--',linewidth=1.2,label='VGG19_acc')plt.plot(epoch, vgg19_val_acc, 'g--',linewidth=1.2, label='VGG19_val_acc')plt.plot(epoch,des121_acc,'b.:' ,linewidth=1.2, label='DesNet201_acc')plt.plot(epoch, des121_val_acc,'b:' ,linewidth=1.2, label='DesNet201_val_acc')#以下两行设置全局字体,在本例中,坐标轴刻度和图例均用新罗马字体来表示plt.rcParams['font.sans-serif']=['TimesNewRoman'] # ['SimSun']宋体;['SimHei']黑体,有很多自己都可以设置plt.rcParams['axes.unicode_minus'] = Falseplt.xlabel('训练回合',fontsize=9.5,fontfamily="SimSun")#设置坐标轴标签,标签大小,局部字体设置,本例设置为宋体plt.ylabel('准确率',fontsize=9.5,fontfamily="SimSun")plt.legend(loc='best',fontsize=7.5,frameon=True)#图例设置,函数中的参数loc除了取best(自动将图例放到合适的位置)外,还可以认为设置,比如右上角时:loc='upper right',左下角时:loc='lower left',#还有upper left、lower right、center left、center right、lower center、upper center等plt.show()