Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

python - How do I display multiple images from one cell in a Jupyter notebook

I'm trying to display a series of images in a cell in a Jupyter notebook. The code looks like this:

from matplotlib import pyplot
file_names = ['a', 'b', 'c']
for name in file_names:
  image = cv2.imread(name)
  pyplot.imshow(image)

But what I get in this notebook cell is only the last image displayed, not each of the three. If I remove the loop and display each image in a separate cell I see all the images. Is there a use of imshow I'm missing?

question from:https://stackoverflow.com/questions/65909655/how-do-i-display-multiple-images-from-one-cell-in-a-jupyter-notebook

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function pyplot.imshow only writes the image to the buffer that will be shown/stored/etc in subsequent actions (this is how you can use pyplot.title, pyplot.xlim, and other commands in a sequence and then only have one plot at the end of all of them).

The reason it seems to display an image in Jupyter is because it's the last line of code executed in the cell, and Jupyter always tries to render the last item it sees unless that behavior is disabled (note that pyplot.imshow actually returns an image object which could be rendered -- Jupyter has logic in place which attempts to do so).

If you really just want to display those items in a loop (as opposed to using subplots or some other way to construct a composite image) then you need to add an additional pyplot.show() command:

from matplotlib import pyplot
file_names = ['a', 'b', 'c']
for name in file_names:
  image = cv2.imread(name)
  pyplot.imshow(image)
  pyplot.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...