说明
??一般做完实验就是画图,实验结果多采用条形图,这里整理下自己的画图模板。图是否丰富是由数据的维度来支撑的,这里按数据维度的大小画出简单条形图与多维并列条形图,注释很详细,直接上代码。
??论文画图一般要求:
- 全英,标题首字母大写,实词大写
- 一般不为彩印,因此要以黑白填充为主
??画图总结,同系列其他文章请浏览:
- 【数据处理】 python 极速极简画图(黑白)——简单条形图、多维并列条形图
- 【数据处理】 python 极速极简画图——频数(率)分布直方图
- 【数据处理】 python 极速极简画图——二维连线、散点图
- 【数据处理】 python 极速极简画图——折线图
简单条形图
??此模板X和Y都是1维的,适用于在横坐标的数据集下,单个方法在单个问题上的表现。
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl#如全英图使用:
mpl.rcParams['font.sans-serif']=['Times New Roman'] #设置字体#如要显示出中文使用:
# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus'] = Falsedef draw(x_data,y_data,title,xytitle,is_showval):plt.figure()bar_width=0.3 #条的宽度plt.bar(x=x_data, height=y_data,color="w",edgecolor="k", alpha=0.8, width=bar_width,hatch="\\\\") #画图并填充if(is_showval): #选择是否显示数值for x, y in enumerate(y_data):plt.text(x, y, '%s' % y, ha='center', va='bottom')plt.title(title,fontsize = 14)plt.xlabel(xytitle[0],fontsize = 14) #横坐标标题plt.ylabel(xytitle[1],fontsize = 14) #纵坐标标题plt.grid(alpha=0.8,linestyle='-.',axis='y') #增加网格线,axis可以是'x','y','both',默认为both# plt.savefig(title+'.jpg', dpi=300) #高清保存plt.show()ydata = [0.0060287,0.00605076,0.00569933,0.00567153,0.00595692]
xdata = [r'$X_'+str(i+1)+'$' for i in range(0,len(ydata))]
xytitle = ['The Meaning of the X-axis /Unit','The Meaning of the Y-axis /Unit']
ptitle = 'The Title of the Picture'
is_showval = 1 #显示数值
draw(xdata,ydata,ptitle,xytitle,is_showval)
??结果展示,Unit表示单位:
多维并列条形图
??此模板X是1维的,Y是多维的,适用于在横坐标的数据集下,多个方法在单个问题上的表现。这里以五个方法为例,经测试,1~5个都是适用的,可以看出,简单条形图只是多维并列条形图的一个特例。
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl#如全英图使用:
mpl.rcParams['font.sans-serif']=['Times New Roman'] #设置字体#如要显示出中文使用:
# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus'] = Falsedef draw2(x_data,y_datas,ptitle,labels,xytitle,is_showval,loc):fig,ax = plt.subplots()bar_width=0.15 #不可随意更改,受图大小,及x、ydata维度限制hatch = ['\\\\\\','///','---','|||','***'] #填充x = np.arange(len(x_data))for i in range(0,len(y_datas)):plt.bar(x+i*bar_width,label = labels[i],height=y_datas[i],color="w",edgecolor="k", alpha=0.8, width=bar_width,hatch=hatch[i])if(is_showval):for i in range(0,len(y_datas)):for x, y in enumerate(y_datas[i]):plt.text(x, y, '%s' % y, ha='center', va='bottom')plt.xticks(np.arange(len(x_data))+(len(y_datas)/2-0.5)*bar_width,x_data) #横坐标plt.tick_params(labelsize=12) #刻度字体大小plt.legend(loc=loc,ncol=len(y_datas),columnspacing =0.05,borderpad=0.2) #label样式plt.title(ptitle,fontsize = 14)plt.xlabel(xytitle[0],fontsize = 14)plt.ylabel(xytitle[1],fontsize = 14)plt.grid(alpha=0.5,linestyle='-.')# plt.savefig(ptitle+'.png', dpi=300,bbox_inches = 'tight')plt.show()ydata = np.array([[0.0060287 ,0.01594343,0.06331985,0.02869123,0.0469503 ,0.07628051],[0.00605076,0.01573297,0.06387128,0.02878903,0.04677676,0.07667354],[0.00569933,0.01567336,0.06227836,0.02670255,0.04563465,0.07510068],[0.00567153,0.01423082,0.06031738,0.02584667,0.04421975,0.07164518],[0.00595692,0.0146612 ,0.06123959,0.02661324,0.04608986,0.07424363]])
xdata = [r'$X_'+str(i+1)+'$' for i in range(0,len(ydata[0]))]
labels = ['M1','M2','M3','M4','M5'] #Method方法
xytitle = ['The Meaning of the X-axis /Unit','The Meaning of the Y-axis /Unit']
ptitle = 'The Title of the Picture'
is_showval = 0 #显示数值
loc = "upper left" #labels显示的位置
draw2(xdata,ydata,ptitle,labels,xytitle,is_showval,loc)