当前位置: 首页 > 知识库问答 >
问题:

边界框问题保存为pdf

百里疏珂
2023-03-14

我需要将matplotlib图形保存到pdf。我遵循Matplotlib howto上的说明,只是不显示结果,而是将其保存为pdf。奇怪的是,pdf画布不受画布大小的影响。相反,保存为png可以在放大的画布上正常工作。

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

def on_draw(event):
    bboxes = []
    for label in labels:
        bbox = label.get_window_extent()
        # the figure transform goes from relative coords->pixels and we
        # want the inverse of that
        bboxi = bbox.inverse_transformed(fig.transFigure)
        bboxes.append(bboxi)

    # this is the bbox that bounds all the bboxes, again in relative
    # figure coords
    bbox = mtransforms.Bbox.union(bboxes)
    if fig.subplotpars.left < bbox.width:
        # we need to move it over
        fig.subplots_adjust(left=1.1*bbox.width) # pad a little
        fig.canvas.draw()
    return False

fig.canvas.mpl_connect('draw_event', on_draw)

plt.savefig("test.pdf", format="pdf")

pdf图像的截屏

更新

plt.tight_layout()

对标题和轴标记执行此操作,但如果图例位于框架外部,则忽略图例,如下图所示。请注意,我将图例放置在图形的右侧。

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
p1, = plt.plot(range(10))
p2, = plt.plot(range(10,0,-1))
ax.set_yticks((2,5,7))
plt.labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
plt.legend([p2, p1], ["line with a loong label", "line with an even longer label, dude!"],\
           loc="center left", bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig("test.pdf", format="pdf")

共有2个答案

闻人和泽
2023-03-14

你可以试试这个例子来解决你的问题。

import matplotlib.pyplot as plt
import numpy as np
sin, cos = np.sin, np.cos

fig = plt.figure(frameon = False)
fig.set_size_inches(5, 8)
ax = plt.Axes(fig, [0., 0., 1., 1.], )
ax.set_axis_off()
fig.add_axes(ax)

x = np.linspace(-4, 4, 20)
y = np.linspace(-4, 4, 20)
X, Y = np.meshgrid(x, y)
deg = np.arctan(Y**3-3*Y-X)
plt.quiver(X, Y, cos(deg), sin(deg), pivot = 'tail', units = 'dots', color = 'red', )
plt.savefig('/tmp/test.png', dpi = 200)

通过将图形设置为5x8英寸,可以生成1000x1600像素的结果图像

fig.set_size_inches(5, 8)
and saving with DPI = 200:

plt.savefig('/tmp/test.png', dpi = 200)
The code to remove the border was taken from here.

(The image posted above is not to scale since 1000x1600 is rather large).
魏凡
2023-03-14

一个可能的黑客是通过替换plt.tight_layout()

plt.tight_layout(rect = [0, 0, 0.4, 1])

但这不是很好。对我有效的是使用参数bbox\u inches

plt.savefig("test.pdf", format="pdf", bbox_inches = 'tight')
 类似资料:
  • 下面是一个简单的冒犯性pdf。 当我运行DrawPrintTextLocations时,下面是我看到的。。 但是据我所知,当您选择文本时,边界框(上图为蓝色)应该代表任何pdf阅读器中显示的灰色区域,如下图所示。 如果pdf阅读器能够计算出高亮显示的灰色区域,就应该能够计算出相同的灰色区域,从而得到字体大小(?)。这个问题适合任何人给我指出正确的方向。 以下是文本“测试线”中“T”的详细信息从其对

  • 问题内容: 假设您具有以下图像: 现在,我想将每个独立字母提取到单个图像中。目前,我已经恢复了轮廓,然后绘制了一个边框,在这种情况下,是针对角色: 之后,我要提取每个框(在本例中为letter )并将其保存到图像文件中。 预期结果: 到目前为止,这是我的代码: 提前致谢。 问题答案: 以下将给您一封信

  • 问题内容: 我有一个div,其border-radius设置为某个值(比方说10px),还有一个嵌套的div,它是其父级的完整宽度和高度。 我注意到,尽管溢出被设置为隐藏,但父母并没有将孩子夹在圆角上。另一个stackoverflow线程指示此行为是“设计使然”: …我不禁注意到“拐角裁剪”部分中的以下描述: 剪切到边框或填充边缘的其他效果(例如“可见”以外的“溢出”)也必须剪切到曲线。替换元素的

  • 我在version上。我正在尝试一个图像,但我得到了一个错误。我做了来查看图像的大小,结果是这样 我所在的服务器空间不足,但它有可用空间,但当我运行

  • 下面是我的函数,它从glyph空间到用户空间进行计算 下面是绘制提取的矩形的函数: 我不知道我做错了什么。有什么想法吗?

  • 在前面的一些章节中,我们介绍了诸多用于图像分类的模型。在图像分类任务里,我们假设图像里只有一个主体目标,并关注如何识别该目标的类别。然而,很多时候图像里有多个我们感兴趣的目标,我们不仅想知道它们的类别,还想得到它们在图像中的具体位置。在计算机视觉里,我们将这类任务称为目标检测(object detection)或物体检测。 目标检测在多个领域中被广泛使用。例如,在无人驾驶里,我们需要通过识别拍摄到