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

Foo*_*Bar 5 python latex matplotlib

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

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

includegraphics[scale=0.5]{myFile.pdf}
Run Code Online (Sandbox Code Playgroud)

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

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

rth*_*rth 5

请参阅这本 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]
    
    Run Code Online (Sandbox Code Playgroud)
  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")
    
    Run Code Online (Sandbox Code Playgroud)
  4. 最后这个数字被导入到 Latex 中,没有任何缩放,

    \includegraphics{myFigure.pdf}
    
    Run Code Online (Sandbox Code Playgroud)

    从而确保,例如,12ptmatplotlib 中的文本对应于 Latex 文档中的相同字体大小。


Bea*_*ber 0

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

\includegraphics[height=4in]{myFile.pdf}
Run Code Online (Sandbox Code Playgroud)

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

\newcommand{\plotstandard}[1]{\centerline{\includegraphics[height=4in]{#1}}}
Run Code Online (Sandbox Code Playgroud)

这就是所谓的使用

\plotstandard{myFile.pdf}
Run Code Online (Sandbox Code Playgroud)