createfig()函数
def create_fig():
plt.figure() # 创建画布
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置字体,不然中文无法显示
plt.rcParams['axes.unicode_minus'] = False # 解决负号不显示问题
plt.rcParams['figure.figsize'] = (8.0, 4.0) # 设置figure_size尺寸
# figsize(12.5, 4) # 设置 figsize
plt.rcParams['savefig.dpi'] = 300 # 保存图片分辨率
plt.rcParams['figure.dpi'] = 300 # 分辨率
# 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600&400
# 指定dpi=200,图片尺寸为 1200*800
# 指定dpi=300,图片尺寸为 1800*1200
plt.rcParams['image.interpolation'] = 'nearest' # 设置 interpolation style
plt.rcParams['image.cmap'] = 'gray' # 设置 颜色 style
图上添加文本框
plt.text(x=(plt.axis()[1] - plt.axis()[0]) * 0.64 + plt.axis()[0],
y=(plt.axis()[3] - plt.axis()[2]) * 0.65 + plt.axis()[2],
s='$\mu=$' + str(mu) + '\n' + '$\sigma=$' + str(sigma) + '\n' + '理论dropout概率:' + str(areaf)+
'\n' + '实际dropout概率:' + str(round(rate,4)),
# 文本内容
fontdict=dict(fontsize=10, ), # 字体属性字典
# 添加文字背景色
bbox={'facecolor': '#74C476', # 填充色
# 'edgecolor': 'b', # 外框色
'alpha': 0.5, # 框透明度
'pad': 8, # 本文与框周围距离
}
)
参考画图程序
import matplotlib.pyplot as plt
plt.figure(figsize=(7,6),dpi=200)
plt.grid()
X=[3,5,7,9,11,13,15,17]
Y_1=[100,96,92,96,100,96,92,96]
Y_2=[96,92,96,100,96,92,96,100]
plt.plot(X,Y_1,label=r"$x_{a,b}^{m,n}$",marker='v',markersize=8,linestyle='-',color='dodgerblue')
plt.plot(X,Y_2,label=r'$x_{a,b}^{m,n} \in \mathbb{R}^{h\times w\times c}$',marker='o',markersize=8,linestyle='-',color='darkorange')
font1={'family' : 'Times New Roman', 'size': 12}
font2={'family' : 'Times New Roman', 'size': 16}
plt.ylabel(r"$x_{a,b}^{m,n}$",fontdict=font2)
plt.xlabel(r"$x_{a,b}^{m,n}$",fontdict=font2)
ax=plt.gca()
xlabel=[r'${3\times 3}$',r'${5\times 5}$',r'${7\times 7}$',r'${9\times 9}$',r'${11\times 11}$',r'${13\times 13}$',r'${15\times 15}$',r'${17\times 17}$']
ylabel=[r'$90$',r'$92$',r'$94$',r'$96$',r'$98$',r'$100$']
plt.tick_params(labelsize=11)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
plt.xticks(range(3,19,2),labels=xlabel)
plt.yticks(range(90,102,2),labels=ylabel)
plt.legend(loc='lower right',prop=font1)
plt.tight_layout()
plt.show()