简体   繁体   中英

pyplot axes title not showing

I have written this code to check object bounding box but when I give title to the axes , it doesn't show up. (I was going to give the file number as title).

#!/home/ckim/anaconda2/bin/python
#%pylab
import os.path as osp
import sys
import cv2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches

def add_path(path):
    if path not in sys.path:
        sys.path.insert(0, path)

def move_figure(f, x, y):
    backend = matplotlib.get_backend()
    f.canvas.manager.window.move(x,y)
    plt.show()

# Add lib to PYTHONPATH
lib_path = osp.join('/home/ckim/Neuro/py-faster-rcnn/', 'lib')
add_path(lib_path)

import datasets
import datasets.pascal_voc as pv

#plt.ion()
fig, ax = plt.subplots(figsize=(8,8))
im = cv2.imread(osp.join('/home/ckim/Neuro/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages/', '{0:06d}'.format(eval(sys.argv[1])) + '.jpg'))
#im = cv2.imread(osp.join('000005.jpg'))
im = im[:, :, (2, 1, 0)]
ax.imshow(im, aspect='equal')
#res = pv._load_pascal_annotation(sys.argv[1])
d = datasets.pascal_voc('trainval', '2007')
res = d._load_pascal_annotation('{0:06d}'.format(eval(sys.argv[1])))

#        return {'boxes' : boxes,
#                'gt_classes': gt_classes,
#                'gt_overlaps' : overlaps,
#                'flipped' : False}

for i in range(len(res['boxes'])):
    x1 = res['boxes'][i][0]
    y1 = res['boxes'][i][1]
    x2 = res['boxes'][i][2]
    y2 = res['boxes'][i][3]
    ax.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1, fill=False, edgecolor='red', linewidth=1.5))
    ax.text(x1, y1 - 5, '{:s}'.format(d._classes[res['gt_classes'][i]]), \
        bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white')
#thismanager = get_current_fig_manager()
#thismanager.window.SetPosition((500, 0))
#thismanager.window.wm_geometry("+500+0")
move_figure(fig, 500, 500)
#fig.show()
#fig.suptitle("Title x")
ax.set_title("Title x")
plt.pause(0)

在此输入图像描述 To test what is the problem, I reduced the code to below, but this abridged version works either for graph plot (case 1) and image display (case 2). I can't find the difference from above code. Could anyone tell me what has gone wrong in above code? (about title now showing)

#!/home/ckim/anaconda2/bin/python

import cv2
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8,8))

# case 1 : plot
#ax.plot([1,2,3,4],[1,4,9,16])

# case 2 : image display
im = cv2.imread('000005.jpg')
im = im[:, :, (2, 1, 0)]
ax.imshow(im, aspect='equal')

ax.set_title("Title x")
plt.pause(0)

在此输入图像描述

There is a call to plt.show() in move_figure which means the figure is shown. As this is a blocking command , no further code will be run until you close this figure. As a result, the title is not set until the figure has disappeared. If you swap the last few lines of you first code as follows,

ax.set_title("Title x")
move_figure(fig, 500, 500)
plt.pause(0)

the title should appear. Alternatively, I'd suggest removing plt.show from move_figure so you can show when you want or savefig etc later on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM