繁体   English   中英

由matplotlib创建的eps文件的LaTeX文档和BoundingBox

[英]LaTeX document and BoundingBox of an eps file created by matplotlib

我在将matplotlib生成的eps文件包含到LaTeX文档中时遇到问题。 似乎没有正确识别图形的大小,并且标题与图形重叠。

请看下面的图片。 这是乳胶文件的图像,其中包括matplotlib生成的图形。 LaTeX源文件和绘图的python源代码如下所示。

========================

LaTeX文档的图像。

=======================

图1.与标题重叠。 似乎LaTeX认为这个数字的尺寸小于实际尺寸。

图2.是与图1相同的eps文件,但是bb参数是在LaTeX文档的includegraphics命令中指定的。 eps文件的BoundingBox是%%BoundingBox: 18 180 594 612 ,并且bb参数被设置为bb=0 0 594 612 前两个值更改为零,同时保留最后两个值。 然后,图2.看起来不错。 图的大小似乎被正确识别。

到目前为止,我在其他计算机上没有这种类型的问题,我想知道是什么导致了这个问题。 我不确定是不是matplotlib或LaTex的问题,我想就如何找到问题的根源提出建议。

matplotlib包的版本是1.1.1rc,OS是Ubuntu 12.04。 我通过latex命令然后使用dvipdfm命令处理了LaTeX文档。

>>> import matplotlib
>>> matplotlib.__version__
'1.1.1rc'

$ latex --version
pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013)
kpathsea version 6.1.1
Copyright 2013 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Compiled with libpng 1.5.16; using libpng 1.5.16
Compiled with zlib 1.2.7; using zlib 1.2.7
Compiled with xpdf version 3.03


$ dvipdfm --version

This is dvipdfmx-20130405 by the DVIPDFMx project team,
modified for TeX Live,
an extended version of dvipdfm-0.13.2c developed by Mark A. Wicks.

Copyright (C) 2002-2013 by the DVIPDFMx project team

This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

这是LaTeX源文件。

\documentclass{article} 
\usepackage[dvips]{graphicx,color}
%\usepackage{amsmath,amssymb}

%\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}



\begin{document}

This is the first paragraph of the text.
Today is a good day. 

\begin{figure}[ht]
\begin{center}
\includegraphics[width=.5\linewidth]{fig.eps}
\caption{This is the caption of the figure included without specifying bb parameters.}
\label{fig1}
\end{center}
\end{figure}

This is the second paragraph of the text written below the first figure environment.
Tomorrow will be a bad day.

\begin{figure}[hb]
\begin{center}
\includegraphics[bb=0 0 594 612, width=.5\linewidth]{fig.eps}
\caption{This is the caption of the figure included with the first two bb parameters set zero.}
\label{fig2}
\end{center}
\end{figure}

% Note that fig.eps has the following bounding box information.
% $ grep BoundingBox fig.eps 
% %%BoundingBox: 18 180 594 612


\end{document}

这是用于绘图的python源代码。

#!/usr/bin/python

import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 2, 4], '-b')
plt.savefig('fig.eps')

首先,您应该使用plt.figure()函数和figsize=(x,y)选项设置图形尺寸。 您还应该使用bbox_inches='tight'选项在plt.savefig()函数中设置边界框,该选项应该删除图周围的额外空格。

您可以尝试的其他一些事情包括将后端设置为'PS',如果您还没有使用:

import matplotlib as mpl
mpl.use('PS') 

另外,我在savefig函数中使用了format='eps'选项,虽然它没有必要,因为你的文件名已经有了eps扩展名,但试一试并没有什么坏处。

我尝试了你的例子,它与dvips一起工作。 然后ps2pdf完成这项工作。 所以dvipdfm可能会对边界框造成错误。

如果您使用OSX,以下命令可能会有所帮助:

$ gs -o temp.ps -sDEVICE = ps2write -dEPSCrop matplotlib.eps

$ ps2eps temp.ps

相关参考: http//syatsin.blogspot.jp/2016/09/how-to-solve-bounding-box-problem-with.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM