1

我使用 matplotlib(来自 git 存储库的最新版本,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 一起安装
  • LaTeX 是使用 MacTeX 安装的
4

0 回答 0