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 つのハイパーリンクと、下にもう 1 つのハイパーリンクを作成することに制限されているため、リンクできなくて申し訳ありません)。

ただし、次のように図を 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 によるものなのかはわかりません。誰かが問題を解決する方法を知っていれば、私は感謝します。pdf2eps などの変換プログラムを使用するのではなく、matplotlib で直接 EPS を作成したいと考えています。また、出版社の規制により、出版には EPS を使用する必要があります。

私の環境は次のとおりです。

  • Mac OS X 10.7.3
  • Python 2.7.2 は Homebrew と共にインストールされます
  • LaTeX は MacTeX を使用してインストールされます
4

0 に答える 0