关于matplotlib / Python和latex的集成有几个问题,但我找不到以下内容:
当我在latex中包含savefig()
创建的pdf文件时,我总是
includegraphics[scale=0.5]{myFile.pdf}
比例通常在0.4
或0.5
左右。鉴于我正在创建A5 beamer幻灯片,为此生成正确大小的pdf文件的正确方法是什么,这样我就不需要在latex中指定它?
请注意,这些不是全尺寸图片专用的投影幻灯片,它需要稍微小一些,以允许页眉,页脚和标题。
答案 0 :(得分:1)
请参阅this matplotlib cookbook以获取Latex的发布质量图。基本上,为了保持所有图形属性(图例等)的恒定大小,应该避免在乳胶中导入数字时进行缩放。建议的方法如下,
\showthe\columnwidth
在python中定义一个辅助函数,用于计算图形大小,
def get_figsize(columnwidth, wf=0.5, hf=(5.**0.5-1.0)/2.0, ):
"""Parameters:
- wf [float]: width fraction in columnwidth units
- hf [float]: height fraction in columnwidth units.
Set by default to golden ratio.
- columnwidth [float]: width of the column in latex. Get this from LaTeX
using \showthe\columnwidth
Returns: [fig_width,fig_height]: that should be given to matplotlib
"""
fig_width_pt = columnwidth*wf
inches_per_pt = 1.0/72.27 # Convert pt to inch
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height = fig_width*hf # height in inches
return [fig_width, fig_height]
然后使用
生成数字import matplotlib
import matplotlib.pyplot as plt
# make sure that some matplotlib parameters are identical for all figures
params = {'backend': 'Agg',
'axes.labelsize': 10,
'text.fontsize': 10 } # extend as needed
matplotlib.rcParams.update(params)
# set figure size as a fraction of the columnwidth
columnwidth = # value given by Latex
fig = plt.figure(figsize=get_figsize(columnwidth, wf=1.0, hf=0.3)
# make the plot
fig.savefig("myFigure.pdf")
最后,这个数字在Latex中导入,没有任何缩放,
\includegraphics{myFigure.pdf}
因此保证,例如,matplotlib中的12pt
文本对应于Latex文档中的相同字体大小。
答案 1 :(得分:0)
最好指定所需图像的绝对大小,而不是缩放比例。 (您可以指定高度,如果更方便,可以使用cm作为单位)
goto
更好的是,定义一个插入图像的宏。这是一个插入图像而不将其包装在图形中的图像(包括标题,填充空间等)
\includegraphics[height=4in]{myFile.pdf}
使用
调用\newcommand{\plotstandard}[1]{\centerline{\includegraphics[height=4in]{#1}}}