Yel*_*low 32 python matplotlib
我想知道我如何能够使用matplotlib例如这样的东西并排绘制图像:
我得到的最接近的是:
这是通过使用此代码生成的:
f, axarr = plt.subplots(2,2)
axarr[0,0] = plt.imshow(image_datas[0])
axarr[0,1] = plt.imshow(image_datas[1])
axarr[1,0] = plt.imshow(image_datas[2])
axarr[1,1] = plt.imshow(image_datas[3])
Run Code Online (Sandbox Code Playgroud)
但我似乎无法让其他图像显示出来.我认为必须有一个更好的方法来做到这一点,因为我想象试图管理索引将是一个痛苦.我查看了文档,虽然我有一种感觉,我可能会看错了.有人能够给我一个例子或指出我正确的方向吗?
Imp*_*est 52
你所面临的问题是,你尝试分配的收益imshow(这是一个matplotlib.image.AxesImage对现有的轴对象.
将图像数据绘制到不同轴的正确方法axarr是
f, axarr = plt.subplots(2,2)
axarr[0,0].imshow(image_datas[0])
axarr[0,1].imshow(image_datas[1])
axarr[1,0].imshow(image_datas[2])
axarr[1,1].imshow(image_datas[3])
Run Code Online (Sandbox Code Playgroud)
所有子图的概念都相同,并且在大多数情况下,轴实例提供的方法与pyplot(plt)接口相同.例如,如果ax是您的子绘图轴之一,用于绘制法线图,ax.plot(..)而不是使用plt.plot().实际上,这可以在您链接到的页面的源代码中找到.
小智 18
您正在一个轴上绘制所有图像.您想要的是单独获取每个轴的手柄并在那里绘制图像.像这样:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.imshow(...)
ax2 = fig.add_subplot(2,2,2)
ax2.imshow(...)
ax3 = fig.add_subplot(2,2,3)
ax3.imshow(...)
ax4 = fig.add_subplot(2,2,4)
ax4.imshow(...)
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请查看此处:http://matplotlib.org/examples/pylab_examples/subplots_demo.html
对于复杂的布局,您应该考虑使用gridspec:http://matplotlib.org/users/gridspec.html
sta*_*010 17
show_image_list()下面是在网格中并排显示图像的完整函数。您可以使用不同的参数调用该函数。
list图像,其中每个图像都是一个 Numpy 数组。默认情况下,它将创建一个包含 2 列的网格。它还会推断每个图像是彩色还是灰度。list_images = [img, gradx, grady, mag_binary, dir_binary]
show_image_list(list_images, figsize=(10, 10))
Run Code Online (Sandbox Code Playgroud)
list图像、list每个图像的标题以及其他参数。show_image_list(list_images=[img, gradx, grady, mag_binary, dir_binary],
list_titles=['original', 'gradx', 'grady', 'mag_binary', 'dir_binary'],
num_cols=3,
figsize=(20, 10),
grid=False,
title_fontsize=20)
Run Code Online (Sandbox Code Playgroud)
这是代码:
import matplotlib.pyplot as plt
import numpy as np
def img_is_color(img):
if len(img.shape) == 3:
# Check the color channels to see if they're all the same.
c1, c2, c3 = img[:, : , 0], img[:, :, 1], img[:, :, 2]
if (c1 == c2).all() and (c2 == c3).all():
return True
return False
def show_image_list(list_images, list_titles=None, list_cmaps=None, grid=True, num_cols=2, figsize=(20, 10), title_fontsize=30):
'''
Shows a grid of images, where each image is a Numpy array. The images can be either
RGB or grayscale.
Parameters:
----------
images: list
List of the images to be displayed.
list_titles: list or None
Optional list of titles to be shown for each image.
list_cmaps: list or None
Optional list of cmap values for each image. If None, then cmap will be
automatically inferred.
grid: boolean
If True, show a grid over each image
num_cols: int
Number of columns to show.
figsize: tuple of width, height
Value to be passed to pyplot.figure()
title_fontsize: int
Value to be passed to set_title().
'''
assert isinstance(list_images, list)
assert len(list_images) > 0
assert isinstance(list_images[0], np.ndarray)
if list_titles is not None:
assert isinstance(list_titles, list)
assert len(list_images) == len(list_titles), '%d imgs != %d titles' % (len(list_images), len(list_titles))
if list_cmaps is not None:
assert isinstance(list_cmaps, list)
assert len(list_images) == len(list_cmaps), '%d imgs != %d cmaps' % (len(list_images), len(list_cmaps))
num_images = len(list_images)
num_cols = min(num_images, num_cols)
num_rows = int(num_images / num_cols) + (1 if num_images % num_cols != 0 else 0)
# Create a grid of subplots.
fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize)
# Create list of axes for easy iteration.
if isinstance(axes, np.ndarray):
list_axes = list(axes.flat)
else:
list_axes = [axes]
for i in range(num_images):
img = list_images[i]
title = list_titles[i] if list_titles is not None else 'Image %d' % (i)
cmap = list_cmaps[i] if list_cmaps is not None else (None if img_is_color(img) else 'gray')
list_axes[i].imshow(img, cmap=cmap)
list_axes[i].set_title(title, fontsize=title_fontsize)
list_axes[i].grid(grid)
for i in range(num_images, len(list_axes)):
list_axes[i].set_visible(False)
fig.tight_layout()
_ = plt.show()
Run Code Online (Sandbox Code Playgroud)
Pre*_*rem 16
如果图像在数组中,并且您想遍历每个元素并打印它,则可以编写如下代码:
plt.figure(figsize=(10,10)) # specifying the overall grid size
for i in range(25):
plt.subplot(5,5,i+1) # the number of images in the grid is 5*5 (25)
plt.imshow(the_array[i])
plt.show()
Run Code Online (Sandbox Code Playgroud)
另请注意,我使用了子图而不是子图。他们都不同
Yel*_*low 11
我发现使用一件事很有帮助:
_, axs = plt.subplots(n_row, n_col, figsize=(12, 12))
axs = axs.flatten()
for img, ax in zip(imgs, axs):
ax.imshow(img)
plt.show()
Run Code Online (Sandbox Code Playgroud)
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(figsize=(4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
for ax, im in zip(grid, image_data):
# Iterating over the grid returns the Axes.
ax.imshow(im)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我大约每周都会访问这个网址一次。对于那些想要一个简单的功能来轻松绘制图像网格的人,我们来吧:
import matplotlib.pyplot as plt
import numpy as np
def plot_image_grid(images, ncols=None, cmap='gray'):
'''Plot a grid of images'''
if not ncols:
factors = [i for i in range(1, len(images)+1) if len(images) % i == 0]
ncols = factors[len(factors) // 2] if len(factors) else len(images) // 4 + 1
nrows = int(len(images) / ncols) + int(len(images) % ncols)
imgs = [images[i] if len(images) > i else None for i in range(nrows * ncols)]
f, axes = plt.subplots(nrows, ncols, figsize=(3*ncols, 2*nrows))
axes = axes.flatten()[:len(imgs)]
for img, ax in zip(imgs, axes.flatten()):
if np.any(img):
if len(img.shape) > 2 and img.shape[2] == 1:
img = img.squeeze()
ax.imshow(img, cmap=cmap)
# make 16 images with 60 height, 80 width, 3 color channels
images = np.random.rand(16, 60, 80, 3)
# plot them
plot_image_grid(images)
Run Code Online (Sandbox Code Playgroud)