Python 使用 matplotlib 并排绘制图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41793931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting images side by side using matplotlib
提问by YellowPillow
I was wondering how I am able to plot images side by side using matplotlib
for example something like this:
我想知道如何使用matplotlib
例如这样的东西并排绘制图像:
The closest I got is this:
我得到的最接近的是这个:
This was produced by using this code:
这是通过使用以下代码生成的:
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])
But I can't seem to get the other images to show. I'm thinking that there must be a better way to do this as I would imagine trying to manage the indexes would be a pain. I have looked through the documentationalthough I have a feeling I may be look at the wrong one. Would anyone be able to provide me with an example or point me in the right direction?
但我似乎无法显示其他图像。我认为必须有更好的方法来做到这一点,因为我认为尝试管理索引会很痛苦。我已经浏览了文档,尽管我觉得我可能看错了。谁能为我提供一个例子或指出我正确的方向?
回答by ImportanceOfBeingErnest
The problem you face is that you try to assignthe return of imshow
(which is an matplotlib.image.AxesImage
to an existing axes object.
你所面临的问题是,你尝试分配的收益imshow
(这是一个matplotlib.image.AxesImage
对现有的轴对象。
The correct way of plotting image data to the different axes in axarr
would be
将图像数据绘制到不同轴的正确方法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])
The concept is the same for all subplots, and in most cases the axes instance provide the same methods than the pyplot (plt) interface.
E.g. if ax
is one of your subplot axes, for plotting a normal line plot you'd use ax.plot(..)
instead of plt.plot()
. This can actually be found exactly in the source from the page you link to.
所有子图的概念都相同,并且在大多数情况下,轴实例提供与 pyplot (plt) 接口相同的方法。例如, ifax
是您的子图轴之一,用于绘制您将使用的法线图ax.plot(..)
而不是plt.plot()
. 这实际上可以在您链接到的页面的源中找到。
回答by Sven Festersen
You are plotting all your images on one axis. What you want ist to get a handle for each axis individually and plot your images there. Like so:
您正在一个轴上绘制所有图像。您想要的是分别获得每个轴的句柄并在那里绘制图像。像这样:
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(...)
For more info have a look here: http://matplotlib.org/examples/pylab_examples/subplots_demo.html
有关更多信息,请查看此处:http: //matplotlib.org/examples/pylab_examples/subplots_demo.html
For complex layouts, you should consider using gridspec: http://matplotlib.org/users/gridspec.html
对于复杂的布局,您应该考虑使用 gridspec:http://matplotlib.org/users/gridspec.html
回答by YellowPillow
One thing that I found quite helpful to use to print all images :
我发现用于打印所有图像非常有帮助的一件事:
_, 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()
回答by Prem
If the images are in an array and you want to iterate through each element and print it, you can write the code as follows:
如果图像在数组中,并且您想遍历每个元素并打印它,则可以编写如下代码:
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()
Also note that I used subplot and not subplots. They're both different
另请注意,我使用了子图而不是子图。他们都不同
回答by Chris Ivan
As per matplotlib's suggestion for image grids:
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()