Grid of images using a loop in Jupyter-Notebook. How?(在 Jupyter-Notebook 中使用循环的图像网格.如何?)
问题描述
我想在 Jupyter Notebook 中以 2x3 矩阵格式显示图像 var_1.png,...,var_40.png
.但是,我只能手动完成:
I want to show images var_1.png,...,var_40.png
in a 2x3 matrix format inside a Jupyter Notebook.
However, I only manage to do it manually:
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')
....
我想要更干净的东西.类似于指定
I want something cleaner. Something like a list comprehension that specifies
image=[img(i)=mpimg.imread('Variable_(i).png') for i in [8,17,28, ..]
[ax[j].imshow(img(j)),ax[j].axis('off') for j in range(len(image))]
一些帮助?
推荐答案
如果列表推导不止一件事,它们很快就会变得不可读.此外,如果列表的内容根本没有被实际使用,那么使用列表推导似乎被认为是不好的风格.
List comprehensions quickly become unreadable if there is more than one thing they do. Also it appears it is considered bad style to use list comprehensions if the content of the list is not actually used at all.
因此我会提出以下建议
import matplotlib.pyplot as plt
images = [plt.imread(f"Variable_{i}.png") for i in [8,17,28,29,31,35]]
fig, axes = plt.subplots(2, 3, sharex=True, sharey=True)
for img, ax in zip(images, axes.flat):
ax.imshow(img)
ax.axis('off')
这篇关于在 Jupyter-Notebook 中使用循环的图像网格.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Jupyter-Notebook 中使用循环的图像网格.如何?


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01