matplotlib将子图组合成一个没有轴,没有间隙的单个图

发布于 2025-02-03 02:08:35 字数 605 浏览 3 评论 0 原文

我的图像看起来像:

”在此处输入图像描述”

它是使用matplotlib生成的:

for slice_idx in range(mandrill_t.highpasses[1].shape[2]):
    print(slice_idx)
    subplot(2, 3, slice_idx+1)
    imshow(np.abs(mandrill_t.highpasses[1][:,:,slice_idx]), cmap='Spectral', clim=(0, 1))

但是,对于我的用例,我希望在没有间隙或轴的单个图像中所有这6张图像 - 我确实可以没有示例输出图像要显示,但是从本质上讲,我希望它们水平堆叠(其中3个)和垂直(其中2个),以便6个图像是一个图像。

我试图四处寻找类似的问题来吸引灵感,但到目前为止还没有运气:(

任何指针都会很棒。

I have an image which looks like so:

enter image description here

It was generated using matplotlib using:

for slice_idx in range(mandrill_t.highpasses[1].shape[2]):
    print(slice_idx)
    subplot(2, 3, slice_idx+1)
    imshow(np.abs(mandrill_t.highpasses[1][:,:,slice_idx]), cmap='Spectral', clim=(0, 1))

However, for my use case, I would like all these 6 images in a single image with no gaps or axis - I do not have an example output image to show, but essentially, I would like them stacked horizontally (3 of them) and vertically (2 of them) so that the 6 images are a single image.

I tried looking around for similar problems to draw inspiration from, but no luck so far :(

Any pointers would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2025-02-10 02:08:35

您必须指定网格参数:

  • 2行
  • 3列
  • 0宽度空间
  • 0高度空间,

matplotlib.pyplot.subplots

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

然后,您可以循环循环越过创建的轴,对于每个轴,您都必须显示图像并设置图像轴至'tight' firtsly和'off'其次:

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

您的代码会很忽略不同,因为您正在为每个 ax AX 绘制不同的图像。

完成代码

import matplotlib.pyplot as plt

img = plt.imread('img.jpeg')

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

plt.show()

“

You have to specify the grid parameters:

  • 2 rows
  • 3 columns
  • 0 width space
  • 0 height space

with matplotlib.pyplot.subplots:

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

Then you can loop over created axes and, for each one of them, you have to show the image and set axis to 'tight' firtsly and 'off' secondly:

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

Your code would be slighlty different, since you are plotting different images for each ax.

Complete Code

import matplotlib.pyplot as plt

img = plt.imread('img.jpeg')

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

plt.show()

enter image description here

本王不退位尔等都是臣 2025-02-10 02:08:35

这就是是(请参阅代码>文档):

只需在开始时添加以下行:

subplots(2, 3, gridspec_kw={"wspace": 0, "hspace": 0})

您可能还必须将一些绘图元素设置为隐形,但很难确切地弄清楚没有MCVE的哪个。

That's what GridSpec is for (see plt.subplots docs):

Just add the following line at the start:

subplots(2, 3, gridspec_kw={"wspace": 0, "hspace": 0})

You might also have to set some plot elements to invisible but it's hard to figure out exactly which without an MCVE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文