如何在 matplotlib 中使用 Latex 在我的轴标题中获得每千位符号?

Osc*_*hen 5 python latex graph matplotlib

直到现在我有这个:

plt.xlabel('$\delta^1^8$O \‰ vs VSMOW') 
Run Code Online (Sandbox Code Playgroud)

它在没有 promille 标志的情况下运行良好,但是当我添加它时会出现一个空白图形。

然后我尝试了 TheImportanceOfBeingErnest 的回答:

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 
Run Code Online (Sandbox Code Playgroud)

但后来出现了:

“UnicodeEncodeError:‘ascii’编解码器无法对位置 264 中的字符‘\u2030’进行编码:序号不在范围内(128)”

也许这有什么问题?

from matplotlib import rc

rc('font', **{'family':'serif','serif':['Palatino']})
    rc('text', usetex=True)
Run Code Online (Sandbox Code Playgroud)

解决方案(感谢 TheImportanceOfBeingErnest)

plt.rcParams['text.latex.preamble']=[r"\usepackage{wasysym}"]
Run Code Online (Sandbox Code Playgroud)

plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW')
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 7

使用普通文本

你需要

  1. 使用 unicode 字符串,即 u"string"
  2. 不转义字符,即存在,但\‰不存在。

所以

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 
Run Code Online (Sandbox Code Playgroud)

产生

在此处输入图片说明

使用乳胶

Latex 没有内置 permille 标志。两种方法是

  • 在文本模式下:使用\usepackage{textcomp}包并通过\textperthousand,

    plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW') 
    
    Run Code Online (Sandbox Code Playgroud)
  • 在数学模式下:使用包\usepackage{wasysym}并通过\permil.

    plt.xlabel(r'$\delta^{18}$O $\permil$ vs VSMOW') 
    
    Run Code Online (Sandbox Code Playgroud)

    使用第一个包并使用\text{\textperthousand}内部数学模式也应该有效。

在此处输入图片说明

有关如何将这些包放入 matplotlib,请阅读如何在 Matplotlib 中编写自己的 LaTeX 序言?