Unicode 不显示在 png 中
Unicode not displaying in png
我有一个函数可以将指数从 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 发行版决定它不应该安装到通常的地方。
确保路径中包含以下内容:
- Ghostscript
- kpsewhich
- dvipng
然后设置以下参数:
rcParams['text.usetex']=True
rcParams['text.latex.unicode']=True
这很慢,而且不理想,因为字体很糟糕。但它至少可以防止框出现在输出中。
我有一个函数可以将指数从 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 发行版决定它不应该安装到通常的地方。
确保路径中包含以下内容:
- Ghostscript
- kpsewhich
- dvipng
然后设置以下参数:
rcParams['text.usetex']=True
rcParams['text.latex.unicode']=True
这很慢,而且不理想,因为字体很糟糕。但它至少可以防止框出现在输出中。