web-dev-qa-db-ja.com

ラテックス出力と同じフォント(-style、-sizeなど)をmatplotlib出力で取得するにはどうすればよいですか?

python module matplotlibによって1つのグラフが作成される1つの.tex- documentがあります。必要なのは、グラフが次のようにドキュメントに溶け込むことですしたがって、グラフで使用される文字は、ドキュメントの他の部分と同じように表示されます。

私の最初の試みは次のようになります(matplotlibrc-ファイル):

text.usetex   : True
text.latex.preamble: \usepackage{lmodern} #Used in .tex-document
font.size    : 11.0 #Same as in .tex-document
backend: PDF

matplotlibのPDF出力が含まれる.texのコンパイルには、pdflatexが使用されます。

これで、出力は悪くないように見えますが、多少異なって見えます。グラフの文字はストローク幅が弱く見えます。

これに対する最善のアプローチは何ですか?

編集:最小の例:LaTeX-Input:

\documentclass[11pt]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\includegraphics{./graph}
\caption{Excitation-Energy}
\label{fig:graph}
\end{figure}

\end{document}

Pythonスクリプト:

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1,2,3,4])
plt.xlabel("Excitation-Energy")
plt.ylabel("Intensität")
plt.savefig("graph.pdf")

PDF出力:

enter image description here

35
John H. K.

フォントの違いは、matplotlibで画像を設定するパラメーターが正しくないか、最終的なドキュメントへの統合が間違っていることが原因である可能性があります。 text.latex.preamble:\ usepackage {lmodern}の問題だと思います。このことは非常にうまくいかず、開発者でさえその動作性を保証していません ここでの見つけ方 。私の場合、それはまったく機能しませんでした。

フォントファミリに関連付けられたフォントの違いが最小限。修正するには、この必要があります: 'font.family': 'lmodern'inrc。他のオプションとより詳細な設定を見つけることができます こちら

この問題を抑制するために、私は少し異なる方法を使用しました-直接。 plt.rcParams ['text.latex.preamble'] = [r "\ usepackage {lmodern}"]。奇妙ではありませんが、うまくいきました。詳細については、上のリンクを参照してください。


これらの影響を防ぐには、このコードを確認することをお勧めします。

import matplotlib.pyplot as plt

#Direct input 
plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
#Options
params = {'text.usetex' : True,
          'font.size' : 11,
          'font.family' : 'lmodern',
          'text.latex.unicode': True,
          }
plt.rcParams.update(params) 

fig = plt.figure()

#You must select the correct size of the plot in advance
fig.set_size_inches(3.54,3.54) 

plt.plot([1,2,3,4])
plt.xlabel("Excitation-Energy")
plt.ylabel("Intensität")
plt.savefig("graph.pdf", 
            #This is simple recomendation for publication plots
            dpi=1000, 
            # Plot will be occupy a maximum of available space
            bbox_inches='tight', 
            )

そして最後にラテックスに移ります:

\documentclass[11pt]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{graphicx}

\begin{document}

\begin{figure}
    \begin{center}
        \includegraphics{./graph}
        \caption{Excitation-Energy}
        \label{fig:graph}
    \end{center}
\end{figure}

\end{document}

結果

Zoom of pdf document

2つのフォントの比較からわかるように、違いはありません(1-MatPlotlib、2-pdfLaTeX) Comparison of fonts

23
Elenium

または、Matplotlibの PGFバックエンド を使用することもできます。これは、LaTeXパッケージPGFを使用してグラフをエクスポートします。次に、それはLaTeXコマンドの単なるコレクションであるため、ドキュメントが使用するのと同じフォントを使用します。次に、includegraphicsの代わりにinputコマンドを使用して、Figure環境に追加します。

\begin{figure}
  \centering
  \input{your_figure.pgf}
  \caption{Your caption}
\end{figure}

サイズを調整する必要がある場合は、パッケージadjustboxが役立ちます。

4
WDiniz