如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

时间:2022-02-02 06:49:12

I have one .tex-document in which one graph is made by the python module matplotlib. What I want is, that the graph blends in to the document as good as possible. So I want the characters used in the graph to look exactly like the other same characters in the rest of the document.

我有一个.tex文件,其中一个图由python模块matplotlib制作。我想要的是,图表尽可能地融入文档。所以我希望图中使用的字符看起来与文档其余部分中的其他字符完全相同。

My first try looks like this (the matplotlibrc-file):

我的第一次尝试看起来像这样(matplotlibrc文件):

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

For compiling of the .tex in which the PDF output of matplotlib is included, pdflatex is used.

为了编译包含matplotlib的PDF输出的.tex,使用pdflatex。

Now, the output looks not bad, but it looks somewhat different, the characters in the graph seem weaker in stroke width.

现在,输出看起来不错,但看起来有些不同,图形中的字符在笔划宽度上看起来较弱。

What is the best approach for this?

对此最好的方法是什么?

EDIT: Minimum example: LaTeX-Input:

编辑:最小例子:LaTeX输入:

\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-Script:

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 output:

如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

2 个解决方案

#1


16  

The difference in the fonts can be caused by incorrect parameter setting out pictures with matplotlib or wrong its integration into the final document. I think problem in text.latex.preamble: \usepackage{lmodern}. This thing works very badly and even developers do not guarantee its workability, how you can find here. In my case it did not work at all.

字体的差异可能是由于使用matplotlib设置图片的参数不正确或者错误将其集成到最终文档中引起的。我认为text.latex.preamble中的问题:\ usepackage {lmodern}。这件事非常糟糕,甚至开发人员也不保证其可行性,你怎么能在这里找到。就我而言,根本不起作用。

Minimal differences in font associated with font family. For fix this u need: 'font.family' : 'lmodern' in rc. Other options and more detailed settings can be found here.

与字体系列相关的字体差异最小。为了解决这个问题,你需要:'font.family':rc中的'lmodern'。其他选项和更详细的设置可以在这里找到。

To suppress this problem, I used a slightly different method - direct. plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]. It is not strange, but it worked. Further information can be found at the link above.

为了解决这个问题,我使用了一种略有不同的方法 - 直接。 plt.rcParams [ 'text.latex.preamble'] = [R “\ usepackage {lmodern}”]。这并不奇怪,但它奏效了。更多信息可以在上面的链接中找到。


To prevent these effects suggest taking a look at this code:

为了防止这些影响建议看一下这段代码:

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', 
            )

And finally move on to the latex:

最后转到乳胶:

\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}

Results

如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

As can be seen from a comparison of two fonts - differences do not exist (1 - MatPlotlib, 2 - pdfLaTeX) 如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

从两种字体的比较可以看出 - 差异不存在(1 - MatPlotlib,2 - pdfLaTeX)

#2


1  

Alternatively, you can use Matplotlib's PGF backend. It exports your graph using LaTeX package PGF, then it will use the same fonts your document uses, as it is just a collection of LaTeX commands. You add then in the figure environment using input command, instead of includegraphics:

或者,您可以使用Matplotlib的PGF后端。它使用LaTeX包PGF导出您的图形,然后它将使用您的文档使用的相同字体,因为它只是LaTeX命令的集合。然后使用输入命令在图形环境中添加,而不是includegraphics:

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

If you need to adjust the sizes, package adjustbox can help.

如果您需要调整尺寸,包调整盒可以提供帮助。

#1


16  

The difference in the fonts can be caused by incorrect parameter setting out pictures with matplotlib or wrong its integration into the final document. I think problem in text.latex.preamble: \usepackage{lmodern}. This thing works very badly and even developers do not guarantee its workability, how you can find here. In my case it did not work at all.

字体的差异可能是由于使用matplotlib设置图片的参数不正确或者错误将其集成到最终文档中引起的。我认为text.latex.preamble中的问题:\ usepackage {lmodern}。这件事非常糟糕,甚至开发人员也不保证其可行性,你怎么能在这里找到。就我而言,根本不起作用。

Minimal differences in font associated with font family. For fix this u need: 'font.family' : 'lmodern' in rc. Other options and more detailed settings can be found here.

与字体系列相关的字体差异最小。为了解决这个问题,你需要:'font.family':rc中的'lmodern'。其他选项和更详细的设置可以在这里找到。

To suppress this problem, I used a slightly different method - direct. plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]. It is not strange, but it worked. Further information can be found at the link above.

为了解决这个问题,我使用了一种略有不同的方法 - 直接。 plt.rcParams [ 'text.latex.preamble'] = [R “\ usepackage {lmodern}”]。这并不奇怪,但它奏效了。更多信息可以在上面的链接中找到。


To prevent these effects suggest taking a look at this code:

为了防止这些影响建议看一下这段代码:

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', 
            )

And finally move on to the latex:

最后转到乳胶:

\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}

Results

如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

As can be seen from a comparison of two fonts - differences do not exist (1 - MatPlotlib, 2 - pdfLaTeX) 如何在matplotlib输出中获得与乳胶输出相同的字体(-style,-size等)?

从两种字体的比较可以看出 - 差异不存在(1 - MatPlotlib,2 - pdfLaTeX)

#2


1  

Alternatively, you can use Matplotlib's PGF backend. It exports your graph using LaTeX package PGF, then it will use the same fonts your document uses, as it is just a collection of LaTeX commands. You add then in the figure environment using input command, instead of includegraphics:

或者,您可以使用Matplotlib的PGF后端。它使用LaTeX包PGF导出您的图形,然后它将使用您的文档使用的相同字体,因为它只是LaTeX命令的集合。然后使用输入命令在图形环境中添加,而不是includegraphics:

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

If you need to adjust the sizes, package adjustbox can help.

如果您需要调整尺寸,包调整盒可以提供帮助。