【问题标题】:Read saved images and produce a single common table figure读取保存的图像并生成单个通用表格图形
【发布时间】:2022-10-23 20:35:16
【问题描述】:

我用不同的 python 脚本创建了 6 个 png 图。

由相同脚本创建的绘图示例:

import numpy as np
import matplotlib.pyplot as plt

plot_num=6
for num in np.arange(plot_num):
    fig, ax = plt.subplots()
    x=np.arange(10)
    y=np.random.rand(10,)
    plt.plot(x,y, marker='o',mfc='red')
    plt.savefig('plot_'+str(num)+'.png')

我想阅读保存的绘图并生成一个 3(列)* 2(行)的常见图形。

最好的解决方案是什么?

下面的代码大致显示了我想要的,但它显示了额外的轴,我不知道如何调整绘图之间的垂直和水平距离。

import matplotlib.pyplot as plt
from PIL import Image
from IPython.display import Image, display

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

filenames=['plot_{}.png'.format(i) for i in range(6)] 

for i in range(6):
    with open(filenames[i],'rb') as f:
        image=Image.open(f)
        ax[i%2][i//2].imshow(image)

display(fig)

【问题讨论】:

  • 您能否澄清“显示附加轴”的含义?我发现问题特别是水平对齐,但显示的所有轴看起来都属于单个图。

标签: python matplotlib plot


【解决方案1】:

Matplotlibs subplot functions 可能只是你的口味。但是,据我所知,它们用于创作。

编辑:重新阅读您的问题:是否可以将您的其他 python 脚本用作该脚本的库,然后将每个单独的脚本添加为子图?

【讨论】:

    【解决方案2】:
    import matplotlib.pyplot as plt
    
    my_dpi=300
    fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(4,2), dpi=my_dpi)
    
    plt.subplots_adjust(left=0.01,
                    bottom=0.1,
                    right=0.9,
                    top=0.9,
                    wspace=-0.1,
                    hspace=-0.3)
    
    filenames=['plot_{}.png'.format(i) for i in range(6)] 
    
    for i in range(6):
        with open(filenames[i],'rb') as f:
            image=plt.imread(f)
            ax[i%2][i//2].axis('off')
            ax[i%2][i//2].imshow(image)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      相关资源
      最近更新 更多