在jupyter笔记本中显示图像网格

时间:2017-11-27 09:58:57

标签: python html matplotlib jupyter-notebook

我有一个包含495行网址的列的数据框。我想在jupyter笔记本中将这些URL显示为图像网格。此处显示了数据帧的第一行。任何帮助表示赞赏。

id           latitude     longitude     owner             title                          url
23969985288 37.721238   -123.071023 7679729@N07 There she blows!    https://farm5.staticflickr.com/4491/2396998528...

我尝试过以下方式,

from IPython.core.display import display, HTML
for index, row in data1.iterrows():
  display(HTML("<img src='%s'>"%(i["url"])))

但是,运行上面的代码会显示消息

> TypeError                         Traceback (most recent call last)
<ipython-input-117-4c2081563c17> in <module>()
      1 from IPython.core.display import display, HTML
      2 for index, row in data1.iterrows():
----> 3   display(HTML("<img src='%s'>"%(i["url"])))

TypeError: string indices must be integers

3 个答案:

答案 0 :(得分:4)

在Jupyter笔记本中显示图像网格的最佳方法是使用matplotlib创建网格,因为您还可以使用matplotlibimshow轴上绘制图像。< / p>

我使用的是3x165网格,因为它确实是495。随意改变网格的尺寸。

import urllib
f, axarr = plt.subplots(3, 165)
curr_row = 0
for index, row in data1.iterrows():
     # fetch the url as a file type object, then read the image
     f = urllib.request.urlopen(row["url"])
     a = plt.imread(f)

     # find the column by taking the current index modulo 3
     col = index % 3
     # plot on relevant subplot
     axarr[col,curr_row].imshow(a)
     if col == 2:
         # we have finished the current row, so increment row counter
         curr_row += 1

答案 1 :(得分:4)

您将IPython.core.display与HTML结合使用的想法是实现此类任务的最佳方法。 matplotlib在绘制大量图像(尤其是将其作为URL的图像)时效率极低。
我根据此概念构建了一个小包装-名为ipyplot

import ipyplot

images = data1['url'].values
labels = data1['id'].values

ipyplot.plot_images(images, labels, img_width=150)

您将获得类似于以下内容的情节:
enter image description here

答案 2 :(得分:0)

我只能用“蛮力”来做:

但是,我只能手动进行:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

%matplotlib inline

img1=mpimg.imread('Variable_8.png')
img2=mpimg.imread('Variable_17.png')
img3=mpimg.imread('Variable_18.png')
          ...

fig, ((ax1, ax2, ax3), (ax4,ax5,ax6)) = plt.subplots(2, 3, sharex=True, sharey=True) 

ax1.imshow(img1)
ax1.axis('off')
ax2.imshow(img2)
ax2.axis('off')
   ....

不知道ot是否有帮助