【问题标题】:How to "zoom out" a plot in matplotlib, keeping all the size ratios the same, but reducing the size in inches?如何在 matplotlib 中“缩小”绘图,保持所有尺寸比例相同,但以英寸为单位减小尺寸?
【发布时间】:2017-09-08 15:27:36
【问题描述】:

我正在使用 matplotlib 进行绘图,我遇到的一个问题是标准绘图尺寸似乎具有可笑的巨大尺寸(以英寸为单位)。 MPL 的默认 dpi 值似乎设置为100dpi,但绘图的大小为 6+ 英寸以上。问题是,如果我将其保存为 pdf,则直接大小太大而无法放在科学论文上(我宁愿将其设置为“列大小”,就像一英寸或两英寸宽)。

不过,如果直接指定figsize就好了

fig, ax = plt.subplots(figsize=[2.,3.])

字体大小和标记大小以英寸为单位保持相同大小,导致相对较大的图形项目和丑陋的图形。我更喜欢默认出现的相同比例,但减小到不同的英寸大小。

据我所知,有两种解决方法。将图形保存为标准尺寸(大约 8 x 6 英寸或其他尺寸),然后在将其插入 LaTeX 时对其进行缩放。或者手动浏览所有图形元素,并更改字体大小、标记大小和图形大小以使其完美。

在某些情况下你不能做前者,而后者是极其乏味的。我必须弄清楚每个图形元素的正确大小,以保持与默认比例相同。

那么,如果有另一个快速的选项来改变图形大小,同时还可以从默认情况下缩放所有内容?

【问题讨论】:

  • 然后start here 并考虑使用 seaborn 的样式表,如果您很懒惰并且可以接受其他一些美学风格。我也不认为在使用正确的格式(矢量化的;不是基于像素的)时使用太大的绘图应该不会有问题,因为您以后可以随时调整它们的大小而不会降级。但这在一定程度上取决于您的下一个管道;字与乳胶。我有一段时间没有写乳胶了,但 pdf 不是我喜欢的格式(eps,svg?)。
  • 所以就我而言,我通常使用 LateX 中的 scale 参数调整大小。但是,目前我正在为您不应该这样做的期刊这样做。
  • 有趣(那个被禁止的选项)。行。然后,您可能想按照我的链接中的说明调整其他尺寸。但也许一些用户稍后会提供帮助。
  • 另外,eps 似乎不允许透明度。

标签: python python-3.x matplotlib plot scipy


【解决方案1】:

通常,为整个脚本设置不同的字体大小就足够了。

import matplotlib.pyplot as plt
plt.rcParams["font.size"] =7

完整示例:

import matplotlib.pyplot as plt
plt.rcParams["font.size"] =7


fig, ax = plt.subplots(figsize=[2.,3.], dpi=100)
ax.plot([2,4,1], label="label")
ax.set_xlabel("xlabel")
ax.set_title("title")
ax.text( 0.5,1.4,"text",)
ax.legend()

plt.tight_layout()
plt.savefig("figure.pdf", dpi="figure")
plt.savefig("figure.png", dpi="figure")
plt.show()

【讨论】:

  • 谢谢,但这仍然会导致绘图边框过厚,我仍然需要手动将所有标记大小设置为适当的值。
  • 您可以使用各自的 rcParams 调整所有线宽和标记大小。例如。 lines.markersize, lines.linewidth, axes.linewidth, xtick.major.size, xtick.minor.size, xtick.major.width, xtick.minor.width.
【解决方案2】:

您可能想询问期刊是否使用width=\columnwidth,例如,

\includegraphics[width=\columnwidth]{example-image}

是可以接受的。虽然这是重新缩放,但它适应当前列 宽度,所以这可能对期刊很友好。

如果没有,也许可以问他们(或其他在该领域发表过文章的作者) 期刊)对于典型论文用来包含图形的 LaTeX 样本,或 关于如何生成图形的建议。

也可能是期刊根本不希望光栅图像 缩放up,因为这会使图像看起来模糊。缩小规模可能不是 光栅图像的问题,缩放非光栅图像可能根本不是问题。


如果一切都失败了,您可以使用 LaTeX 本身来重新调整由 matplotlib。下面,make_plot 函数(取自matplotlib example gallery)生成 /tmp/tex_demo.pdfrescale_pdf 函数重新调整 tex_demo.pdf 和 将结果写入/tmp/tex_demo-scaled.pdfrescale_pdf 函数 通过subprocess 模块调用pdflatex

调整LaTeX模板中的paperheightpaperwidth参数

paperheight=2.25in, 
paperwidth=3in, 

控制重新缩放的pdf图形的大小。


import subprocess
import os
import textwrap
import numpy as np
import matplotlib.pyplot as plt

def make_plot(filename):
    """https://matplotlib.org/users/usetex.html"""
    # Example data
    t = np.arange(0.0, 1.0 + 0.01, 0.01)
    s = np.cos(4 * np.pi * t) + 2

    # fig, ax = plt.subplots(figsize=(2, 3))

    plt.rc('text', usetex=True)
    plt.rc('font', family='serif')
    fig, ax = plt.subplots()
    ax.plot(t, s)

    ax.set_xlabel(r'\textbf{time} (s)')
    ax.set_ylabel(r'\textit{voltage} (mV)',fontsize=16)
    ax.set_title(r"\TeX\ is Number "
              r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
              fontsize=16, color='gray')
    # Make room for the ridiculously large title.
    plt.subplots_adjust(top=0.8)

    plt.savefig(filename)

def rescale_pdf(filename):
    stub, ext = os.path.splitext(filename)
    print('begin rescale')
    template = textwrap.dedent(r'''
        \documentclass{article}

        %% https://tex.stackexchange.com/a/46178/3919
        \usepackage[
                paperheight=2.25in, 
                paperwidth=3in, 
                bindingoffset=0in, 
                left=0in,
                right=0in,
                top=0in,
                bottom=0in, 
                footskip=0in]{geometry}
        \usepackage{graphicx}

        \begin{document}
        \pagenumbering{gobble}
        \begin{figure}
        %% https://tex.stackexchange.com/questions/63221/how-to-deal-with-pdf-figures
        \includegraphics[width=\linewidth]{%s}
        \end{figure}

        \end{document}''' % (stub,))

    tex_filename = 'result.tex'
    with open(tex_filename, 'w') as f:
        f.write(template)

    proc = subprocess.Popen(['pdflatex', '-interaction', 'batchmode', tex_filename])
    output, err = proc.communicate()

filename = '/tmp/tex_demo.pdf'
dirname = os.path.dirname(filename)
if dirname: 
    os.chdir(dirname)
make_plot(filename)
rescale_pdf(filename)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多