使用matplotlib绘制png网格

2024-09-28 22:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这个主题上发现了多个类似的问题,但到目前为止,我无法根据我的需要调整任何解决方案,因此很抱歉重新发布

我正在尝试使用matplotlib绘制png图像的网格,最接近我想要的是使用下面的代码,可以在这里找到https://matplotlib.org/stable/gallery/axes_grid1/simple_axesgrid.html

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

im1 = np.arange(100).reshape((10, 10))
im2 = im1.T
im3 = np.flipud(im1)
im4 = np.fliplr(im2)

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, [im1, im2, im3, im4]):
    # Iterating over the grid returns the Axes.
    ax.imshow(im)

plt.show()

我的问题是,如何去除x和y记号/标签,并为每个图像指定一个标题

再次,我很抱歉重复这个问题


Tags: 图像importmatplotlibasnpfigpltgrid
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:08

此代码

import matplotlib.pyplot as plt

image = plt.imread("sample.png")

fig, axes = plt.subplots(2, 3)

for row in [0, 1]:
    for column in [0, 1, 2]:
        ax = axes[row, column]
        ax.set_title(f"Image ({row}, {column})")
        ax.axis('off')
        ax.imshow(image)

plt.show()

将会产生

enter image description here

相关问题 更多 >