I am working on identifying tumors in CT images, using SciPy. Once I’ve identified the slice containing the tumor, I retrieve the slice using:
slice_x, slice_y = ndimage.find_objects(label_im==label)[0]
I then plot the full image as follows:
plt.imshow(image, cmap='gray')
I want to overlay the contour of the extracted slice (containing the tumor) on top of the original image in the appropriate location. However, I can’t get the contour plot to lie on top of the proper region. Instead, the contour plot always lies in the top-left corner of the image. Here’s what I’m using:
plt.contour(mask[slice_x, slice_y], position=(slice_x.start, \
slice_y.start), linewidths=2, colors='r')
Here is the result:

How can I set the position of the contour plot inside the main plot? Thanks!
You need to use the
extentkeyword argument instead ofposition. However, you’ll need to specify a maximum as well as a start (i.e. it needs to beextent=[xmin, xmax, ymin, ymax])In your case, it would look something like:
Note that you may need to use the
originkeyword argument (or flip ymin and ymax) to control the way the input data is interpreted.Alternately, you could do something like the following:
It will be a bit inefficient if you’re working with a large region, but it’s much cleaner.
As a full example of the latter approach: