【问题标题】:Bold font weight for LaTeX axes label in matplotlibmatplotlib 中 LaTeX 轴标签的粗体字重
【发布时间】:2013-01-14 18:38:39
【问题描述】:

matplotlib 中,您可以将轴标签的文本设置为粗体

plt.xlabel('foo',fontweight='bold')

你也可以在正确的后端使用 LaTeX

plt.xlabel(r'$\phi$')

然而,当你将它们组合在一起时,数学文本不再是粗体了

plt.xlabel(r'$\phi$',fontweight='bold')

以下 LaTeX 命令似乎也没有任何效果

plt.xlabel(r'$\bf \phi$')
plt.xlabel(r'$\mathbf{\phi}$')

如何在坐标轴标签中添加粗体 $\phi$

【问题讨论】:

    标签: python matplotlib latex


    【解决方案1】:

    很遗憾,您不能使用粗体字体来加粗符号,请参阅 tex.stackexchange 上的 this question

    正如答案所示,您可以使用 \boldsymbol 来加粗 phi:

    r'$\boldsymbol{\phi}$'
    

    您需要将amsmath 加载到 TeX 序言中:

    matplotlib.rc('text', usetex=True)
    matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
    

    【讨论】:

    • 这不起作用:ValueError: \boldsymbol{\phi} ^ Unknown symbol: \boldsymbol (at char 0), (line:1, col:1) 也许它需要我们需要 amsmath 加载?你在你的机器上测试过这个吗?
    • @Hooked 我认为包括序言应该按照here:matplotlib.rc('text', usetex=True),matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"] 的描述工作。不幸的是我还不能测试,当我有的时候会更新。
    • 在 Matpotlib 3.3.3 和 Python 3 7.13.0 上,将这些命令添加到 rcParams 会在尝试绘图时导致以下错误:! LaTeX Error: File type1ec.sty not found.
    • @MRule - 参见 github.com/matplotlib/matplotlib/issues/16911 - 在 Debian 和衍生产品上:sudo apt install cm-super - 在 macOS 上:sudo tlmgr install cm-super - 在其他系统上:参见 repology.org/projects/?search=cmsuperrepology.org/projects/?search=cm-super 或 @987654326 列出的软件包@
    • matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"] 对我不起作用(Python 3.9.9),但matplotlib.rc('text.latex', preamble=r'\usepackage{amsmath}') 起作用了!感谢this post
    【解决方案2】:

    如果您打算在整个情节中使用一致的粗体字体,最好的方法可能是启用乳胶并将\boldmath 添加到您的序言中:

    # Optionally set font to Computer Modern to avoid common missing font errors
    matplotlib.rc('font', family='serif', serif='cm10')
    
    matplotlib.rc('text', usetex=True)
    matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']
    

    那么你的轴或图形标签可以有任何数学乳胶表达式,并且仍然是粗体:

    plt.xlabel(r'$\frac{\phi + x}{2}$')
    

    但是,对于非数学标签部分,您需要将它们明确设置为粗体:

    plt.ylabel(r'\textbf{Counts of} $\lambda$'}
    

    【讨论】:

      【解决方案3】:

      如果有人像我一样从 Google 偶然发现这个,另一种不需要调整 rc 序言(并且与非乳胶文本冲突)的方法是:

      ax.set_ylabel(r"$\mathbf{\partial y / \partial x}$")
      

      【讨论】:

      • 这是一个很好的答案!
      【解决方案4】:

      正如Latex on python: \alpha and \beta don't work? 的回答所指出的那样。您可能对\b 有疑问,因此\boldsymbol 可能无法按预期工作。在这种情况下,您可以在 python 代码中使用类似:'$ \\\boldsymbol{\\\beta} $'。前提是您使用序言plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

      【讨论】:

      • 感谢您的输入(关于一个 4 岁的问题!),但在字符串前使用 r 应该有助于避免 \b。 @AndyHayden 提供的答案可以正常工作并确定问题。
      【解决方案5】:

      当使用 LaTeX 排版图形中的所有文本时,您可以使用 \textbf 将“普通”(非方程式)文本加粗:

      ax.set_title(r"\textbf{some text}")
      

      【讨论】:

        【解决方案6】:

        这些解决方案都不适合我,我惊讶地发现如此简单的事情却如此令人气愤地实现。最后,这对我的用例有用。我会建议调整它以供您自己使用:

        plt.suptitle(r"$ARMA({0}, {1})$ Multi-Parameter, $\bf{{a}}$, Electrode Response".format(n_i, m), fontsize=16)

        {0}{1} 指的是提供给format 方法的位置参数,这意味着0 指的是变量n_i1 指的是变量m

        注意:在我的设置中,由于某种原因,\textbf 不起作用。我在某处读到 \bf 在 LaTeX 中已被弃用,但对我来说这是有效的。

        【讨论】:

          猜你喜欢
          • 2020-09-24
          • 1970-01-01
          • 2015-06-28
          • 1970-01-01
          • 2018-05-26
          • 2016-10-09
          • 2013-04-23
          • 1970-01-01
          相关资源
          最近更新 更多