线型,参考:
【python】Matplotlib作图常用marker类型、线型和颜色_python_巫_月-华为云开发者联盟 (csdn.net)
添加参数 markevery=10,可控制 marker 的间隔。
代码
import matplotlib.pyplot as plt
import pandas as pd
# 绘制PR
def plot_PR():
pr_csv_dict = {
# 'YOLOv5m': r'F:\ChromeDown\yolov5-6.1-pruning-autodl\yolov5-6.1-pruning-autodl\runs\val\exp\PR_curve.csv',
# 'YOLOv7': r'G:\pycharmprojects\yolov7-distillation\runs\test\exp\PR_curve.csv',
# 'YOLOv7-tiny': r'G:\pycharmprojects\yolov7-distillation\runs\test\exp2\PR_curve.csv',
# 'YOLOv8s': r'G:\pycharmprojects\ultralytics-main\runs\detect\yolov8s-from-ultralytics-main-bs111\PR_curve.csv',
'Once-Smoke': r'E:\YOLO\results\P-R\exp58-1_PR_curve-smoke.csv',
'Once-Fire': r'E:\YOLO\results\P-R\exp58-1_PR_curve-fire.csv',
'Once-All': r'E:\YOLO\results\P-R\exp58-1_PR_curve-all.csv',
'Twice-Smoke': r'E:\YOLO\results\P-R\6_PR_curve-smoke.csv',
'Twice-Fire': r'E:\YOLO\results\P-R\6_PR_curve-fire.csv',
'Twice-All': r'E:\YOLO\results\P-R\6_PR_curve-all.csv',
}
line_dict = {
'Once-Smoke': 'ro--',
'Once-Fire': 'c<-',
'Once-All': 'bs-',
'Twice-Smoke': 'p-',
'Twice-Fire': 'y>-',
'Twice-All':'g^-'
}
# 绘制pr
fig, ax = plt.subplots(1, 1, figsize=(8, 6), tight_layout=True)
for modelname in pr_csv_dict:
res_path = pr_csv_dict[modelname]
x = pd.read_csv(res_path, usecols=[1]).values.ravel()
data = pd.read_csv(res_path, usecols=[4]).values.ravel()
ax.plot(x, data, line_dict[modelname],label=modelname, linewidth='2',markevery=60)
# 添加x轴和y轴标签
ax.set_xlabel('Recall',fontsize=18)
ax.set_ylabel('Precision',fontsize=18)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# 设置横纵坐标的刻度字体
plt.xticks(fontsize=18) # 设置横坐标刻度字体大小为12
plt.yticks(fontsize=18) # 设置纵坐标刻度字体大小为12
plt.legend(bbox_to_anchor=(1.04, 1), loc='upper left',fontsize=14)
plt.grid() # 显示网格线
# 显示图像
fig.savefig("P-R.png", dpi=250)
plt.show()
from numpy.random import randn
# 线形测试
def plot_line():
data = randn(30).cumsum()
# plt.plot(data, 'ro--', drawstyle='steps-post')
plt.plot(data, 'ro--',markevery=10)
plt.show()
if __name__ == '__main__':
# 设置全局字体为 Times New Roman
plt.rcParams['font.family'] = 'times new roman'
plot_PR() # 绘制PR
# plot_line()