Python Unicode未在png中显示

Python Unicode未在png中显示,python,unicode,matplotlib,Python,Unicode,Matplotlib,我有一个函数,它将指数从1e+N更改为1x10^N,其中N将是unicode上标的N: # -*- coding: utf-8 -* try: unicode except: unicode = str _superscripts = u'⁻⁰ⁱ²³⁴⁵⁶⁷⁸⁹' _superscript_map = dict(zip(map(ord, u'-0123456789'), _superscripts)) def to_fancy(number, fmt='e'): as

我有一个函数,它将指数从1e+N更改为1x10^N,其中N将是unicode上标的N:

# -*- coding: utf-8 -*
try:
    unicode
except:
    unicode = str

_superscripts = u'⁻⁰ⁱ²³⁴⁵⁶⁷⁸⁹'
_superscript_map = dict(zip(map(ord, u'-0123456789'), _superscripts))

def to_fancy(number, fmt='e'):
    as_str = ('{:' + fmt + '}').format(number)
    as_str, _, exponent = as_str.partition('e')
    if exponent:
        exponent = unicode(int(exponent.replace('+', '')))
        exponent = exponent.translate(_superscript_map)
        as_str += u'×10' + exponent
    return as_str
如果我随后使用此值绘制带有标题的图形,即:

plt.figure(1)
plt.title(to_fancy(1e+18))
输出在xwindow上看起来很好(这是使用MacOSx后端)

但当我以正常方式将图形保存为.png文件时,它会将unicode呈现为空框:


多亏了上面提供的提示,终于找到了修复方法。排序的最简单方法是使用TeX-unicode格式。我以前尝试过这个,但由于路径问题,它不起作用,我的TeXShop发行版决定它不应该安装到通常的地方

确保路径中包含以下内容:

  • 鬼书
  • Kpsewich
  • dvipng
  • 然后设置以下参数:

    rcParams['text.usetex']=True
    rcParams['text.latex.unicode']=True
    

    这很慢,也不理想,因为字体很糟糕。但它至少可以防止框出现在输出中

    这大概是在matplotlib?啊,是的。抱歉,我知道我遗漏了一些内容。您可能希望在此处使用而不是
    str.format()
    ;因此,将
    ('{:'+fmt+'}').format(number)
    替换为
    format(number,fmt)
    。更简单。啊,追溯到前面的一个问题。让安蒂完成了任务,谢谢。是的,我从他那里继承了密码。