Joh*_* K. 32 python matplotlib tex
我有一个.tex
文档,其中一个图由python模块生成matplotlib
.我想要的是,图表尽可能好地融入文档.所以我希望图中使用的字符看起来与文档其余部分中的其他字符完全相同.
我的第一次尝试看起来像这样(matplotlibrc
-file):
text.usetex : True
text.latex.preamble: \usepackage{lmodern} #Used in .tex-document
font.size : 11.0 #Same as in .tex-document
backend: PDF
Run Code Online (Sandbox Code Playgroud)
用于编译包含.tex
PDF输出的matplotlib
内容pdflatex
.
现在,输出看起来不错,但看起来有些不同,图中的字符在笔画宽度上看起来较弱.
对此最好的方法是什么?
编辑:最小例子: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}
Run Code Online (Sandbox Code Playgroud)
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")
Run Code Online (Sandbox Code Playgroud)
PDF输出:
小智 23
字体的差异可能是由于使用matplotlib设置不正确的参数或错误地将其集成到最终文档中引起的.我认为text.latex.preamble中的问题:\ usepackage {lmodern}.这件事非常糟糕,甚至开发人员也不保证其可行性,你怎么能在这里找到.就我而言,根本不起作用.
与字体系列相关的字体差异最小.为了解决这个问题u需要:'font.family': 'lmodern'的RC.其他选项和更详细的设置可以在这里找到.
为了解决这个问题,我使用了一种略有不同的方法 - 直接.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',
)
Run Code Online (Sandbox Code Playgroud)
最后转到乳胶:
\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}
Run Code Online (Sandbox Code Playgroud)
从两种字体的比较可以看出 - 差异不存在(1 - MatPlotlib,2 - pdfLaTeX)
或者,您可以使用 Matplotlib 的PGF 后端。它使用 LaTeX 包 PGF 导出您的图表,然后它将使用您的文档使用的相同字体,因为它只是 LaTeX 命令的集合。然后使用输入命令而不是 includegraphics 在图形环境中添加:
\begin{figure}
\centering
\input{your_figure.pgf}
\caption{Your caption}
\end{figure}
Run Code Online (Sandbox Code Playgroud)
如果您需要调整尺寸,调整盒包可以提供帮助。
tikzplotlib就是为了这个目的而出现的。而不是savefig()
使用
import tikzplotlib
tikzplotlib.save("out.tex")
Run Code Online (Sandbox Code Playgroud)
并将生成的文件包含在您的 LaTeX 文档中
\input{out.tex}
Run Code Online (Sandbox Code Playgroud)
如果您在创建文件后需要更改绘图中的内容,也可以轻松编辑。
归档时间: |
|
查看次数: |
12906 次 |
最近记录: |