Matplotlib手册

时间:2024-04-03 19:43:21

plt.text添加文本注释

参考:https://www.jianshu.com/p/3f2cba11b771
例如绘制如下图片:
Matplotlib手册
生成这种带字母的文本框真的让我想半天,这提供一种思路,采用text文本注释,但是没有上述图片那么好看,读者如果有更好的方法欢迎评论

plt.text(x, y , str , family=' ' , size= , color= ,style= , weight= , bbox=dict(facecolor= , alpha=0.2))

参数名 参数功能
x,y 设置文本的起始坐标
str 文本的字符串内容
family 设置字体的样式如宋体等
size 设置大小
color 设置颜色
style 设置样式如倾斜
weight 设置粗体等款细
bbox 设置文本的边框,在dict()中设置参数
facecolor设置边框颜色,alpha设置透明度

具体参考手册:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html#matplotlib.pyplot.text
参考代码如下:

# -*- coding: UTF-8 -*-
import numpy as np
import matplotlib.pyplot as plt
 
N = 7 # 点的个数
x = [0.1,0.2,0.3,0.4,0.5,0.6,0.7]
y = [0.1,0.2,0.3,0.4,0.5,0.6,0.7]
n1 = ['A','B','C','D','E','F','G']

n = np.arange(7)

colors = np.random.rand(N) # 随机产生50个0~1之间的颜色值
area = np.pi * (15 * np.array([0.5]*7))**2 

for i in range(7):
    plt.text(x[i],y[i], n1[i], size = 8,\
            family = "fantasy", color = "black", style = "italic", weight = "light",\
            bbox = dict(boxstyle="square",facecolor = "w", alpha = 1))

plt.show()

结果如下:
Matplotlib手册

plt一图多曲线绘制,添加图例

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
# 数据老子瞎编的
N = 6
x = [12,85,85,28,182,172]
y = [25.1,28.0,29.9,28.8,33.2,36.2]
n1 = ['A','B','C','D','E','F']
# 数据老子瞎编的
plt.xlim((0,200))
plt.ylim((22,40))
for i in range(6):
    plt.text(x[i],y[i], n1[i], size = 10,\
            family = "fantasy", color = "black", style = "normal", weight = "black",\
            bbox = dict(boxstyle="square",facecolor = "w", alpha = 1))
plt.plot([22,29,51],[28.2,31.0,33],'gv',linestyle='-',label='YOLOv3')
plt.plot([64,72,98,121,153],[30.5,32.5,34.3,35.1,35.7],'bo',linestyle='-',label='RetinaNet-50')
plt.plot([35,50,65,80,125,160],[22.7,23.5,26,28.9,32.7,34.7],'c+',linestyle='-',label='Faster-RCNN')
plt.plot([15,30,45],[29.7,33.5,36,],'r*',linestyle='-',label='FuckNet')
# plt.legend(loc='upper left')
plt.legend()
plt.xlabel('inference time (ms)')
plt.ylabel('COCO AP')
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.savefig('fig1.pdf',bbox_inches = 'tight')
plt.show()

Matplotlib手册
至于图里面插入表格的操作,我是真的不会,还没搞定