使用matplotlib给论文绘图

2022/08/22 学术 Python 共 2913 字,约 9 分钟

最近开始水一篇论文,好像还是第一次自己给论文做图,我把方法记录下来:

  1. 最省事的是直接看matplotlib官方的cheatsheet,这应该是最快的速成方法了吧:🔗Matplotlib cheatsheets and handouts

  2. 感觉很多第三方包用于论文、出版美化,试了好几个包我都不太喜欢,暂时没有发现特别好用的,感觉matplotlib本身是够用的。

  3. 想要图好看,可以参考其他漂亮论文(可以选一些大厂出品的论文,审美都在线,什么Google,Facebook之类)的配色,我就依葫芦画瓢选的配色,下面的几个图我都觉得挺好看的(出自论文Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation):

    image-20220822135454893image-20220822135835052
    三个颜色的搭配:红绿蓝;圆、三角形和正方形两个颜色的搭配:红蓝;两类:实线虚线;圆、正方形
    image-20220822135941877image-20220822140040524
    红色和绿色搭配也很好看!红、黄、蓝一样有眼前一亮的感觉
  4. 分析我们得到的数据类型,如果已经是csv, txt这样结构化的数据,直接用pandas做;json应该也很简单,不要绕远路!

下面是我这次做图的代码,我的数据是训练完后得到的result.csv

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 如果你已经明确了用哪些列做图,可以不用导入全部,用use_col指定特定的列就好
results = pd.read_csv("results/results.csv")
results.head()
epochtrain/box_losstrain/obj_losstrain/cls_lossmetrics/precisionmetrics/recallmetrics/mAP_0.5metrics/mAP_0.5:0.95val/box_lossval/obj_lossval/cls_lossx/lr0x/lr1x/lr2
000.0333190.02546600.932970.819640.914210.630310.0173510.01241700.0702910.0033010.003301
110.0273040.01740400.895330.805610.903970.569750.0198140.01430900.0402690.0066120.006612
220.0257370.01717600.921790.803100.907280.610650.0201630.01390100.0102250.0099020.009902
330.0267070.01813600.646180.396790.476590.214910.0312750.02719200.0099010.0099010.009901
440.0276790.01658000.789140.677350.738990.428540.0265460.01878400.0099010.0099010.009901
# 这里的列名还是很烦的,确认一下列名到底是怎样的
results.columns
Index(['               epoch', '      train/box_loss', '      train/obj_loss',
       '      train/cls_loss', '   metrics/precision', '      metrics/recall',
       '     metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', '        val/box_loss',
       '        val/obj_loss', '        val/cls_loss', '               x/lr0',
       '               x/lr1', '               x/lr2'],
      dtype='object')
# 把列名全改了!
results.rename(columns = {'               epoch':'epoch', '      train/box_loss':'train_box_loss', 
                          '      train/obj_loss':'train_obj_loss', '   metrics/precision':'precision', 
                          '      metrics/recall':'recall', '     metrics/mAP_0.5':'mAP_0.5', 
                          'metrics/mAP_0.5:0.95':'mAP_0.5:0.95','        val/box_loss':'val_box_loss',
                         '        val/obj_loss':'val_obj_loss'}, inplace = True)
# 读入另外一个模型的数据,做比较
results_anno = pd.read_csv("results/results_anno.csv")
results_anno.rename(columns = {'               epoch':'epoch', '      train/box_loss':'train_box_loss', 
                          '      train/obj_loss':'train_obj_loss', '   metrics/precision':'precision', 
                          '      metrics/recall':'recall', '     metrics/mAP_0.5':'mAP_0.5', 
                          'metrics/mAP_0.5:0.95':'mAP_0.5:0.95','        val/box_loss':'val_box_loss',
                         '        val/obj_loss':'val_obj_loss'}, inplace = True)
# 合并loss
results["loss"] = results["train_obj_loss"] + results["train_box_loss"]
results_anno["loss"] = results_anno["train_obj_loss"] + results_anno["train_box_loss"]
# plt.subplots()可以做到多个小图,不过这里不需要
fig, ax = plt.subplots()
# 'o-'确定了画的是实线,点是实心圆,markevery确定多少个epoch做一次圆圈标记,color可以自己去选,我在cheatsheet里找的
ax.plot(results['epoch'], results['loss'],"o-", color="C3", markevery=50, 
         label='With Copy-Paste')
# 's-'确定了画的是实线,点是方形
ax.plot(results_anno['epoch'], results_anno['loss'], 's-',  color="C0", markevery=50,
        label='Without Copy-Paste')
ax.set_xlabel('Epoch')
ax.set_ylabel('Loss')
# autoscale()可以自动确定画出的图x轴和y轴的取值范围
plt.autoscale()
ax.legend()
# grid就是画方格线
ax.grid()
plt.show()
# 放论文的图存到pdf文件,再将pdf插入latex里
fig.savefig("loss.pdf")

最后的效果:

image-20220822140953404

文档信息

Search

    Table of Contents