[英]Loading multiple images with Ipython display in Jupyter Notebook
[英]Display multiple images in jupyter notebook
我有一个图像位置列表
image_locations = [path/car.jpg, path/tree.jpg, ..., path/apple.jpg]
每个图像都有不同的大小和形状。
在 Jupyter 笔记本中显示/打印这些图像(最好是在某些网格中)的最佳方式是什么?
您可以使用模块pyplot
、 image
和 function subplots subplots()
:
# Import modules
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Read images
img1 = mpimg.imread('myImage1.png')
img2 = mpimg.imread('myImage2.png')
img3 = mpimg.imread('myImage3.png')
img4 = mpimg.imread('myImage4.png')
# Set your canvas (fig) and the number of axes (images) you want to display
# In this example I want to display my 4 images in a grid 2x2
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2,ncols=2,figsize=(15,10))
ax1.imshow(img1)
ax1.set_title("Image 1")
ax2.imshow(img2)
ax2.set_title("Image 2")
ax3.imshow(img3)
ax3.set_title("Image 3")
ax4.imshow(img4)
ax4.set_title("Image 4")
plt.show()
# The same example with just the first two images in a grid 1x2
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(15,10))
ax1.imshow(img1)
ax1.set_title("Image 1")
ax2.imshow(img2)
ax2.set_title("Image 2")
plt.show()
Fig
只是您的axes
的容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.