使用 matplotlib 并排绘制图像 [英] Plotting images side by side using matplotlib

查看:72
本文介绍了使用 matplotlib 并排绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 matplotlib 并排绘制图像,例如:

I was wondering how I am able to plot images side by side using matplotlib for example something like this:

我得到的最接近的是这个:

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 documentation although 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?

请参见 answer >@duhaime 如果你想要一个函数来自动确定网格大小.

See the answer from @duhaime if you want a function to automatically determine the grid size.

推荐答案

您面临的问题是您尝试分配 imshow(这是一个matplotlib.image.AxesImage 到现有的轴对象.

The problem you face is that you try to assign the return of imshow (which is an matplotlib.image.AxesImage to an existing axes object.

axarr 中将图像数据绘制到不同轴的正确方法是

The correct way of plotting image data to the different axes in axarr would be

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])

所有子图的概念都相同,并且在大多数情况下,轴实例提供与 pyplot (plt) 接口相同的方法.例如.如果 ax 是您的子图轴之一,要绘制法线图,您将使用 ax.plot(..) 而不是 plt.plot().实际上,可以在您链接到的页面的源代码中找到它们.

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.

这篇关于使用 matplotlib 并排绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆