您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

matplotlib绘制符合论文要求的图片

2024/3/14 4:54:31发布27次查看
最近需要将实验数据画图出来,由于使用python进行实验,自然使用到了matplotlib来作图。
下面的代码可以作为画图的模板代码,代码中有详细注释,可根据需要进行更改。
1 # -*- coding: utf-8 -*- 2 import numpy as np 3 import matplotlib.pyplot as plt 4 plt.rcparams['font.sans-serif']=['arial']   #如果要显示中文字体,则在此处设为:simhei 5 plt.rcparams['axes.unicode_minus']=false    #显示负号 6  7 x = np.array([3,5,7,9,11,13,15,17,19,21]) 8 a = np.array([0.9708, 0.6429, 1, 0.8333, 0.8841, 0.5867, 0.9352, 0.8000, 0.9359, 0.9405]) 9 b= np.array([0.9708, 0.6558, 1, 0.8095, 0.8913, 0.5950, 0.9352, 0.8000, 0.9359, 0.9419])10 c=np.array([0.9657, 0.6688, 0.9855, 0.7881, 0.8667, 0.5952, 0.9361, 0.7848, 0.9244, 0.9221])11 d=np.array([0.9664, 0.6701, 0.9884, 0.7929, 0.8790, 0.6072, 0.9352, 0.7920, 0.9170, 0.9254])12 13 #label在图示(legend)中显示。若为数学公式,则最好在字符串前后添加$符号14 #color:b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、15 #线型:-    --     -.    :       , 16 #marker:.    ,      o      v        <       *        +        117 plt.figure(figsize=(10,5))18 plt.grid(linestyle = --)            #设置背景网格线为虚线19 ax = plt.gca()20 ax.spines['top'].set_visible(false)   #去掉上边框21 ax.spines['right'].set_visible(false) #去掉右边框22 23 plt.plot(x,a,color=black,label=a algorithm,linewidth=1.5)24 plt.plot(x,b,k--,label=b algorithm,linewidth=1.5)25 plt.plot(x,c,color=red,label=c algorithm,linewidth=1.5)26 plt.plot(x,d,r--,label=d algorithm,linewidth=1.5)27 28 group_labels=['dataset1','dataset2','dataset3','dataset4','dataset5',' dataset6','dataset7','dataset8','dataset9','dataset10'] #x轴刻度的标识29 plt.xticks(x,group_labels,fontsize=12,fontweight='bold')  #默认字体大小为1030 plt.yticks(fontsize=12,fontweight='bold')31 plt.title(example,fontsize=12,fontweight='bold')        #默认字体大小为1232 plt.xlabel(data sets,fontsize=13,fontweight='bold')33 plt.ylabel(accuracy,fontsize=13,fontweight='bold')34 plt.xlim(3,21)                  #设置x轴的范围35 #plt.ylim(0.5,1)36 37 #plt.legend()                   #显示各曲线的图例38 plt.legend(loc=0, numpoints=1)39 leg = plt.gca().get_legend()40 ltext  = leg.get_texts()41 plt.setp(ltext, fontsize=12,fontweight='bold')  #设置图例字体的大小和粗细42 43 plt.savefig('d:\\filename.svg',format='svg')    #建议保存为svg格式,再用inkscape转为矢量图emf后插入word中44 plt.show()
下面是上面代码绘制的图例:
建议保存图片的格式svg(因为matplotlib存为eps矢量图时候会有问题),然后使用inkscape软件将svg格式转为emf矢量图格式。如果svg图片很多,可以在windows下使用批处理(安装inkscape软件后要记得设置path路径),下面这段代码能将它所在目录下的svg文件转为emf文件。将下面代码复制到文本文件,改后缀名为bat。
1 @echo off2 for %%i in (*.svg) do (3     echo %%i4     inkscape -f %%i -m %%~ni.emf5 )6 @echo finished
以上就是matplotlib绘制符合论文要求的图片的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product