如何在matplotlib中使用Latex在我的轴标题中获得每千米标志?

时间:2017-05-29 12:05:09

标签: python matplotlib graph latex

到现在为止我有这个:

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

没有promille符号它运行正常,但是当我添加它时会出现一个空白图表。

然后我尝试了TheImportanceOfBeingErnest的答案:

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

然后出现了这个:

" UnicodeEncodeError:' ascii'编解码器无法编码字符' \ u2030'位置264:序数不在范围内(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')

1 个答案:

答案 0 :(得分:2)

使用普通文本

你需要

  1. 使用unicode字符串,即u"string"
  2. 没有逃避角色,即存在,但\‰没有。
  3. 所以

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

    产生

    enter image description here

    使用乳胶

    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}也应该可以。

    enter image description here

    有关如何将这些包装入matplotlib的内容,请阅读How to write your own LaTeX preamble in Matplotlib?