plt 使用小技巧

1.使显示的图与原图尺寸一致

img = plt.imread(imgId)

# keep the origin image size
dpi = 100.0
height, width, depth = img.shape
plt.figure(figsize=(width / dpi, height / dpi))
plt.imshow(img)

2.打标签

plt.annotate('%s:%.3f' % (obj['name'], float(obj['score'])),
              xy=poly[0], xycoords='data', xytext=(+7, +10), textcoords='offset points',
              color='white',
              bbox=dict(facecolor='black', alpha=0.5))

3.保存图片,移除白色 padding

# save result fig to vis directory
plt.savefig(self.basepath + '/vis/' + imgId,
            bbox_inches='tight')  # auto scale fig to img, only in save, not show

只能用于 save,不能用于 show。

4. 只显示 save 后的 tight image

# save result fig to vis directory
save_path = self.basepath + '/vis/' + imgId + '.png'
plt.savefig(save_path, bbox_inches='tight')  # auto scale fig to img, only in save, not show

# show ann img
plt.cla()  # clear fig to show ann img
ann_img = plt.imread(save_path)

# keep the origin image size
dpi = 100.0
height, width, depth = ann_img.shape
plt.figure(figsize=(width / dpi, height / dpi))
plt.axis('off')

plt.imshow(ann_img)
plt.show()

你可能感兴趣的:(plt 使用小技巧)