0

If to put plt.show() out of loop then it's one figure, if it's in the loop then it's a figure appearing one by one after closing previous one. How to make to open all 3 figures together, but not in subplot?

# import the necessary packages

from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
import matplotlib.pyplot as plt
import cv2 as cv

image = img_as_float(cv.imread("/Users/ilsuleym/Documents/DCNNtest/1M10.tif"))

f = plt.figure()
# loop over the number of segments
for numSegments, i in ((100, 1), (200, 2), (300, 3)):

    segments = slic(image, n_segments=numSegments, sigma=5)
    f = plt.imshow(mark_boundaries(image, segments))
    plt.title("%d segments" % (numSegments))
    plt.axis("off")
    plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54

1 Answers1

0

Put the plt.figure() call inside the for loop to create new figure at each iteration.

Jussi Nurminen
  • 2,257
  • 1
  • 9
  • 16
  • I don't completely understand what you are asking. With the code above, it should not close any figures inside the loop. However, the behavior at the end of the script will depend on the environment where you run it. For example, if you run it in Windows shell, the script will just terminate when it gets to the end and all figures will close. – Jussi Nurminen Dec 15 '20 at 15:57
  • In PyCharm, all figures appeared if add plt.figure() inside and plt.show() outside of the loop, but the script is not terminated till I manually closed all figures. – Ilida Suleymanova Dec 15 '20 at 16:00
  • Ok, that's so called blocking behavior. The `plt.show()` call blocks the execution of the script until the figures are closed. – Jussi Nurminen Dec 15 '20 at 16:09
  • So good to know. Can you recommend please how execution can be continued with open figures? – Ilida Suleymanova Dec 15 '20 at 16:24
  • It depends on the environment. You might Google for 'matplotlib non-blocking'. See for example https://stackoverflow.com/questions/28269157/plotting-in-a-non-blocking-way-with-matplotlib I'm using a Jupyter notebook where I don't have this problem at all - execution continues after `plt.show()` – Jussi Nurminen Dec 15 '20 at 16:27