Tha*_*ndu 2 html python matplotlib jupyter-notebook
我有一个包含495行URL的列的数据框。我想在jupyter笔记本中将这些URL显示为图像网格。此处显示数据帧的第一行。任何帮助表示赞赏。
id latitude longitude owner title url
23969985288 37.721238 -123.071023 7679729@N07 There she blows! https://farm5.staticflickr.com/4491/2396998528...
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,
from IPython.core.display import display, HTML
for index, row in data1.iterrows():
display(HTML("<img src='%s'>"%(i["url"])))
Run Code Online (Sandbox Code Playgroud)
但是,运行上述代码会显示消息
> 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
Run Code Online (Sandbox Code Playgroud)
您使用IPython.core.displayHTML 的想法是这种任务的最佳方法。matplotlib在绘制如此大量的图像时效率非常低(尤其是当您将它们作为 URL 时)。
我基于这个概念构建了一个小包——它叫做ipyplot
import ipyplot
images = data1['url'].values
labels = data1['id'].values
ipyplot.plot_images(images, labels, img_width=150)
Run Code Online (Sandbox Code Playgroud)
在Jupyter笔记本中显示图像网格的最佳方法可能是使用matplotlib创建网格,因为您也可以matplotlib使用绘制轴上的图像imshow。
我正在使用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
Run Code Online (Sandbox Code Playgroud)