python - 在 jupyter notebook 中并排显示图像
问题描述
我想在我的 jupyter 笔记本中并排显示带有 matplotlib 的专辑中的一些图像。
我写了一个函数,但它不起作用。
import cv2
import numpy as np
import matplotlib.pyplot as plt
def show(path):
for iter in list.get(path):
img = cv2.imread(images)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.axis('off')
plt.imshow(img)
plt.show()
fig, ax = plt.subplots(nrows=2, ncols=2)
解决方案
您只需要为每个图像使用 subplot 函数:
plt.subplot(2, 2, n) # n is the position of your subplot (1 to 4)
plt.imshow(img)
加载完所有子图后,只需调用:
plt.show()
下面我做了一个简单的例子,可能对你有帮助,你可以在我的github repo中找到这个文件的 Jupyter notebook :
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("a"+".jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.axis('off')
plt.subplot(2, 2, 1)
plt.imshow(img)
plt.subplot(2, 2, 2)
plt.imshow(img)
plt.subplot(2, 2, 3)
plt.imshow(img)
plt.subplot(2, 2, 4)
plt.imshow(img)
plt.show()
结果是:
推荐阅读
- ios - Xcode 应用程序中的 UITabBarItem 存在致命错误,因为在隐式展开可选值时发现 nil。不知道出了什么问题/如何解决
- discord.py - 如何让我的机器人加入语音频道
- c# - 如何在 Unity 3D 中将对象移动到鼠标的 z 和 y 坐标
- postgresql - Postgres Hstore 联系人不能从其他模式工作
- python - 在 Python 中渲染 3 维康威生命游戏的有效方法是什么?
- java - 未创建线程“主”会话中的异常:此版本的 ChromeDriver 仅支持 Chrome 版本 81
- node.js - 使用 Express 和 Sequelize 与 SQL Server 登录失败
- terminal - 为什么 rust 进程会在 Ctrl+C 上导致终端错误?
- c - 使用'void'的彩票生成功能
- java - 使用正则表达式获取整数值