【问题标题】:How do I get a per mille sign in my axis title using Latex in matplotlib?如何在 matplotlib 中使用 Latex 在我的轴标题中获得每千分之一符号?
【发布时间】:2017-10-29 17:43:31
【问题描述】:

到目前为止,我有这个:

plt.xlabel('$\delta^1^8$O \‰ vs VSMOW') 

没有 promille 标志它运行良好,但是当我添加它时会出现一个空白图表。

然后我尝试了 TheImportanceOfBeingErnest 的回答:

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 

但后来出现了:

"UnicodeEncodeError: 'ascii' codec can't encode character '\u2030' in position 264: ordinal not in range(128)"

也许这有什么问题?

from matplotlib import rc

rc('font', **{'family':'serif','serif':['Palatino']})
    rc('text', usetex=True)

解决方案(感谢 TheImportanceOfBeingErnest)

plt.rcParams['text.latex.preamble']=[r"\usepackage{wasysym}"]

plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW')

【问题讨论】:

  • 无论你是否使用乳胶(usetex),都需要完全不同的解决方案。但我在回答中已经详细说明了它们。

标签: python matplotlib graph latex


【解决方案1】:

使用普通文本

你需要

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

所以

plt.xlabel(u'$\delta^{18}$O ‰ vs VSMOW') 

生产

使用乳胶

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

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

    plt.xlabel(r'$\delta^{18}$O \textperthousand vs VSMOW') 
    
  • 在数学模式下:使用包\usepackage{wasysym} 并通过\permil 获取它。

    plt.xlabel(r'$\delta^{18}$O $\permil$ vs VSMOW') 
    

    使用第一个包并在数学模式中使用\text{\textperthousand} 应该也可以。

关于如何将这些包放入 matplotlib,请阅读How to write your own LaTeX preamble in Matplotlib?

【讨论】:

  • 感谢您的回答,但我现在明白了:UnicodeEncodeError: 'ascii' codec can't encode character '\u2030' in position 264: ordinal not in range(128) 也许有问题与 rc? (请参阅更新问题)
  • 你没有说你在问题中使用了乳胶。我更新了在乳胶中获取它的方法的答案。
  • 非常感谢,我知道了!
猜你喜欢
  • 2018-05-26
  • 2011-11-17
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2018-07-26
  • 1970-01-01
相关资源
最近更新 更多