参考大佬的博客,记录一下,以后一定会用到
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial'] # 如果要显示中文字体,则在此处设为:SimHei
plt.rcParams['axes.unicode_minus'] = False # 显示负号
# 绘制折线图需要的数据。x为横坐标,y为纵坐标,可以换成自己的数据
x = np.array([1, 2, 3, 4, 5, 6])
VGG_supervised = np.array([2.9749694, 3.9357018, 4.7440844, 6.482254, 8.720203, 13.687582])
VGG_unsupervised = np.array([2.1044724, 2.9757383, 3.7754183, 5.686206, 8.367847, 14.144531])
ourNetwork = np.array([2.0205495, 2.6509762, 3.1876223, 4.380781, 6.004548, 9.9298])
# 定义一个窗口,设置窗口尺寸
plt.figure(figsize=(10, 5))
plt.grid(linestyle="--") # 设置背景网格线为虚线
ax = plt.gca()
ax.spines['top'].set_visible(False) # 去掉上边框
ax.spines['right'].set_visible(False) # 去掉右边框
# 绘制折线图
# label在图示(legend)中显示。若为数学公式,则最好在字符串前后添加"$"符号
# color:b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、
# 线型:- -- -. : ,
# marker:. , o v < * + 1
plt.plot(x, VGG_supervised, marker='<', color="blue", label="VGG Supervised Network", linewidth=1.5, markersize=8)
plt.plot(x, VGG_unsupervised, marker='o', color="green", label="VGG Unsupervised Network", linewidth=1.5, markersize=8)
plt.plot(x, ourNetwork, marker='+', color="red", label="ShuffleNet Network", linewidth=1.5, markersize=8)
group_labels = ['Top 0-5%', 'Top 5-10%', 'Top 10-20%', 'Top 20-50%', 'Top 50-70%', ' Top 70-100%'] # x轴刻度的标识
# 设置xy坐标每一个阶段的标识
plt.xticks(x, group_labels, fontsize=12, fontweight='bold') # 默认字体大小为10
plt.yticks(fontsize=12, fontweight='bold')
# 设置标题
plt.title("example", fontsize=12, fontweight='bold') # 默认字体大小为12
# 设置xy坐标的标签
plt.xlabel("Performance Percentile", fontsize=13, fontweight='bold')
plt.ylabel("4pt-Homography RMSE", fontsize=13, fontweight='bold')
plt.xlim(0.9, 6.1) # 设置x轴的范围
plt.ylim(1.5, 16)
# plt.legend() 用于显示各曲线的图例
plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize=10, fontweight='bold') # 设置图例字体的大小和粗细
# 建议保存为svg格式,再用inkscape转为矢量图emf后插入word中
plt.savefig('./filename.svg', format='svg')
# # 保存文件,dpi指定保存文件的分辨率
# # bbox_inches="tight" 可以保存图上所有的信息,不会出现横纵坐标轴的描述存掉了的情况
# plt.savefig('result/QOS_aver.png', dpi=300, bbox_inches="tight")
# 记住,如果你要show()的话,一定要先savefig,再show。如果你先show了,存出来的就是一张白纸。
plt.show()