繁体   English   中英

Matplotlib 和 Latex 投影仪:正确的尺寸

[英]Matplotlib and latex beamer: Correct size

关于matplotlib/Python和latex的集成问了几个问题,但是我找不到以下内容:

当我在乳胶中包含savefig()创建的 pdf 文件时,我总是

includegraphics[scale=0.5]{myFile.pdf}

并且比例通常在0.40.5左右。 鉴于我正在创建 A5 投影仪幻灯片,生成正确大小的 pdf 文件的正确方法是什么,这样我就不需要在乳胶中指定它?

请注意,这些不是全尺寸的仅图像投影仪幻灯片,它需要稍微小一些以允许页眉、页脚和标题。

请参阅此 matplotlib 食谱以创建 Latex 的出版质量图。 本质上,为了保持所有图形属性(图例等)缩放的恒定大小,应避免在乳胶中导入图形时。 建议的方法如下,

  1. 以像素为单位查找 Latex 文档的线宽。 这可以完成,临时插入文档中的某处, \\showthe\\columnwidth

  2. 在 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]
  3. 然后生成数字,

     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")
  4. 最后这个数字是在没有任何缩放的情况下在 Latex 中导入的,

     \\includegraphics{myFigure.pdf}

    因此,除其他外,确保 matplotlib 中的12pt文本对应于 Latex 文档中的相同字体大小。

您最好指定所需图像的绝对大小,而不是比例。 (您可以指定高度,如果这样更方便,并且可以使用厘米作为单位)

\includegraphics[height=4in]{myFile.pdf}

更好的是,定义一个插入图像的宏。 这是插入图像而不将其包裹在图形中的方法(包括标题、填充空间等)

\newcommand{\plotstandard}[1]{\centerline{\includegraphics[height=4in]{#1}}}

这被称为使用

\plotstandard{myFile.pdf}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM