matplotlib:创建不合并图像对象的PDF

2024-04-27 04:27:11 发布

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

我喜欢使用matplotlib来创建一个PDF,然后我会经常使用adobeillustrator或Inkscape进行调整。不幸的是,当matplotlib将图形保存为PDF时,它会将多个图像合并到一个对象中。在

例如,下面的代码

import matplotlib.pyplot as plt
import numpy as np

im = np.random.rand(10, 10) * 255.0
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
ax.imshow(im, extent = [0,10,0,10])
ax.imshow(im, extent = [12,22,0,10])
ax.set_xlim(-2,24)
fig.savefig('images_get_combined.pdf')

创建以下PDF(在我手动转换为PNG之后,我可以在这里发布) enter image description here 当我在adobeillustrator中打开images_get_combined.pdf时,这两个图像被合并成一个图像。我不能移动一个图像相对于另一个图像。在

我试图使用BboxImage来解决这个问题,但是正如在bug report中所示,BboxImage不能很好地处理PDF后端。也许带有BboxImage错误的PDF后端将得到解决,但是我想知道是否有其他方法可以让PDF后端分别保存每个图像。在


Tags: 图像importpdfmatplotlibasnpfigplt
1条回答
网友
1楼 · 发布于 2024-04-27 04:27:11

您可以尝试另存为多页PDF:

import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

im = np.random.rand(10, 10) * 255.0
with PdfPages('images_get_combined.pdf') as pdf:
    fig = plt.figure()
    ax = fig.add_axes([0.1,0.1,0.8,0.8])
    ax.imshow(im, extent = [0,10,0,10])
    pdf.savefig()
    plt.close()
    fig = plt.figure()
    ax = fig.add_axes([0.1,0.1,0.8,0.8])
    ax.imshow(im, extent = [12,22,0,10])
    pdf.savefig()
    plt.close()

另请参见matplotlib文档中的Multipage PDF示例。在

相关问题 更多 >