簡體   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