无法将使用matplotlib制作的EPS文件正确导入LaTeX

时间:2012-03-15 19:08:40

标签: python latex matplotlib postscript eps

我使用matplotlib(最新版本来自git repository,v1.1.0-538-g8b1e5b8,但最新版本也会出现以下问题)使用以下代码制作PDF和EPS文件。

import numpy as np
import matplotlib.pyplot as plt

def func(x,y):
    return (1- x/2 + x**5 + y**3)*np.exp(-x**2-y**2)

xmin, xmax = -3, 3
ymin, ymax = -1.2, 1.2

x0 = np.linspace(xmin, xmax, 100, endpoint=True)
y0 = np.sin(x0)
x1 = np.linspace(xmin, xmax, 10, endpoint=True)
y1 = np.sin(x1)

npix = 128
x = np.linspace(xmin, xmax, npix, endpoint=True)
y = np.linspace(xmin, xmax, npix, endpoint=True)
X,Y = np.meshgrid(x, y)
img = func(X, Y)

fig = plt.figure(figsize=(15, 3))

ax_img = fig.add_subplot(131)
ax_img.imshow(img, cmap=plt.cm.binary_r, origin='lower', interpolation='nearest', resample=False)
ax_img.axis('off')

ax0 = fig.add_subplot(132)
ax0.plot(x0, y0, ls='-', c='0.4', lw=0.8, label='Test 1')
ax0.set_xlim(xmin, xmax)
ax0.set_ylim(ymin, ymax)
ax0.set_xlabel("This is X-label", fontsize='xx-large')
ax0.set_ylabel("This is Y-label", fontsize='xx-large')

ax2 = fig.add_subplot(133)
ax2.plot(x0, y0, '-', c='0.4', lw=0.8, label='Test 2')
ax2.plot(x1, y1, 'o', c='0.4', lw=0.8, label='Test 2')
ax2.set_xlim(xmin, xmax)
ax2.set_ylim(ymin, ymax)
ax2.set_xlabel("This is X-label", fontsize='xx-large')
ax2.set_ylabel("This is Y-label", fontsize='xx-large')

fig_bottom = 0.05
fig_height = 0.9
fig_img_l  = 0.05
fig_img_w  = 0.15
fig_ax0_l = 0.25
fig_ax0_w = 0.4
fig_ax2_l = 0.7
fig_ax2_w = 0.25

ax_img.set_position([fig_img_l, fig_bottom, fig_img_w, fig_height])
ax0.set_position([fig_ax0_l, fig_bottom, fig_ax0_w, fig_height])
ax2.set_position([fig_ax2_l, fig_bottom, fig_ax2_w, fig_height])

art_txt1 = ax_img.text(0, 1.2, 'Header 1', ha='left', va='center', fontsize='x-large', transform=ax_img.transAxes)
art_txt2 = ax_img.text(0, 1.1, 'Header 2', ha='left', va='center', fontsize='x-large', transform=ax_img.transAxes)

fout_eps = 'mpl_test.eps'
fout_pdf = 'mpl_test.pdf'
plt.savefig(fout_eps, bbox_inches='tight', bbox_extra_artists=[art_txt1, art_txt2])
plt.savefig(fout_pdf, bbox_inches='tight', bbox_extra_artists=[art_txt1, art_txt2])

使用此脚本创建的EPS(http://db.tt/8anYHL2w)和PDF(http://db.tt/r6QSZQp7)文件可以通过gv和Preview.app看到。 (抱歉没有链接,因为我此时仅限于制作2个超链接,下面还有一个。)

但是,当我将图形放在LaTeX中时,EPS的左侧面板上的图像不会出现如下。

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage[cm]{fullpage}
\begin{document}
\begin{figure}[H]
  \begin{center}
    \includegraphics[bb= 0 0 1014.0875 257.796875, clip, width=0.95\linewidth]{mpl_test.pdf}
    \includegraphics[bb= 0 0 1014.0875 257.796875, draft, clip, width=0.95\linewidth]{mpl_test.pdf}
  \end{center}
  \caption{This is what I expect (PDF is used). Bottom panel shows BoundingBox of the figure on top panel by setting ``draft'' in \textbackslash{}includegraphics.}
\end{figure}
\begin{figure}
  \begin{center}
    \includegraphics[width=0.95\linewidth]{mpl_test.eps}
    \includegraphics[draft,width=0.95\linewidth]{mpl_test.eps}
  \end{center}
  \caption{But in reality... (w/ EPS)}
\end{figure}
\end{document}

生成的PDF文件:http://db.tt/J1GPthVO

以PDF格式导入的数字按预期显示,但EPS未显示在matplotlib中带有“imshow”的左侧面板。此外,Y标签的最左侧部分被移除。

我不确定这是否是由matplotlib或LaTeX引起的。如果有人知道如何解决问题,我将不胜感激。我想用matplotlib直接创建EPS,而不是使用转换程序,如pdf2eps。由于出版商的监管,我需要使用EPS进行发布。

我的环境是:

  • Mac OS X 10.7.3
  • Python 2.7.2随Homebrew
  • 一起安装
  • 使用MacTeX安装LaTeX

0 个答案:

没有答案