我遇到了一个问题,与matplotlib用于表示刻度标签的文本对象有关。为了测试目的,我需要检查在绘制中创建的刻度标签的值。如果标签是字符串或正数,则没有问题:返回一个Unicode字符串,我测试它(或根据情况将其转换为数字),一切都很好。但是,如果标签是负数,我收到了无法理解原因的乱码Unicode字符串。让我们看看这个示例代码:
现在,如果我要求获取第二个标签(即
我尝试使用Matplotlib中的非ASCII字符解决字体问题,但结果相同(我不确定它是否应该与可视化问题有关...)。问题Matplotlib中的重音字符也有类似的问题,因为它涉及到可视化而不是值本身。
我开始感到有点迷失了...我知道Python 2.7存在一些Unicode“困难”,但通常我可以以某种方式避免它们。
我知道问题是减号,因为我可以通过暴力替换罪犯来避免问题:
But this is more of a hack than a solution, and I'm almost certain that it's not stable across different systems, so I would like something closer to a solution.
I'm working with:
Thank you very much for your help!
EDIT:
Corrected the order of the plot, as I got the x and y inverted, sorry.
EDIT2:
A similar info is present at this link: http://www.coniferproductions.com/2012/12/17/unicode-character-dump-in-python/ In the end, it shows how in some books the minus sign used is a more aesthetically pleasant one but not recognized by the python interpreter as a valid character.
EDIT3:
谜底揭晓。Matplotlib返回的字符是“MINUS SIGN”,即减号的正确印刷符号。键盘创建的字符实际上是“HYPHEN-MINUS”,这是常用但不是印刷上正确的符号。请参见维基百科的解释http://en.wikipedia.org/wiki/Hyphen-minus。
因此,我使用的简单替换实际上是正确的实用方法,但从“道德”上讲,这是Python(2.7和3.x同样)中的一个错误,它不能识别减号符号的正确表示法。
请参见http://bugs.python.org/issue6632中的错误跟踪。
import pylab as plt
fig, ax = plt.subplots(1)
ax.plot([-1, 0, 1, 2], range(4))
labels = ax.get_xticklabels()
现在,如果我要求获取第二个标签(即
0
)的文本内容,我会得到一个普通的Unicode字符串:labels[1].get_text()
# u'0.0'
但第一个字符(-1
)的Unicode值很奇怪。
labels[1].get_text()
# u'\u22121'
这段文字在终端中打印是正确的,但在这种情况下,我需要将其与数值进行比较,但是每次转换都失败,无论使用 int
还是 float
。
我尝试使用 UTF-8 字符串进行转换,但仍然失败。
text = labels[1].get_text()
text.encode('utf8')
# '\xe2\x88\x921'
但是,这仍然是一个正确打印并在转换时引发错误的问题。我也查看了unicodedata
模块,但似乎它只能转换单个字符,因此在这种情况下无用。我还尝试使用unicodedata.normalize
和任何可能的格式来规范化字符串,但仍然没有成功。
我转向了pipy模块unidecode
(如Python and character normalization所建议的),但同样没有成功。
from unidecode import unidecode
unidecode(text)
# '[?]1'
我尝试使用Matplotlib中的非ASCII字符解决字体问题,但结果相同(我不确定它是否应该与可视化问题有关...)。问题Matplotlib中的重音字符也有类似的问题,因为它涉及到可视化而不是值本身。
我开始感到有点迷失了...我知道Python 2.7存在一些Unicode“困难”,但通常我可以以某种方式避免它们。
我知道问题是减号,因为我可以通过暴力替换罪犯来避免问题:
text.replace(u'\u2212', '-')
# u'-1'
But this is more of a hack than a solution, and I'm almost certain that it's not stable across different systems, so I would like something closer to a solution.
I'm working with:
- python 2.7.3
- matplotlib 1.2.0
- pylab 1.7.0
- IPython 0.13.1
Thank you very much for your help!
EDIT:
Corrected the order of the plot, as I got the x and y inverted, sorry.
EDIT2:
A similar info is present at this link: http://www.coniferproductions.com/2012/12/17/unicode-character-dump-in-python/ In the end, it shows how in some books the minus sign used is a more aesthetically pleasant one but not recognized by the python interpreter as a valid character.
EDIT3:
谜底揭晓。Matplotlib返回的字符是“MINUS SIGN”,即减号的正确印刷符号。键盘创建的字符实际上是“HYPHEN-MINUS”,这是常用但不是印刷上正确的符号。请参见维基百科的解释http://en.wikipedia.org/wiki/Hyphen-minus。
因此,我使用的简单替换实际上是正确的实用方法,但从“道德”上讲,这是Python(2.7和3.x同样)中的一个错误,它不能识别减号符号的正确表示法。
请参见http://bugs.python.org/issue6632中的错误跟踪。
编辑4:
要禁用此行为,在Matplotlib上有一个简单的解决方案,只需修改rcparams,可以在.matplotlibrc或编程方式中进行修改。import matplotlib as mpl
mpl.rcParams['axes.unicode_minus']=False
agg
后端和Arial
字体族保存为pdf
时遇到了这个问题。png
格式的输出正常。你有什么想法吗?你也是因为pdf
格式出了问题吗?(但你的EDIT4对我有效,谢谢!) - aseagram