绘制utf-8标签时出错

时间:2018-04-07 01:11:19

标签: python-2.7 matplotlib utf-8 arabic axis-labels

我正在尝试显示情节,但标签位于utf-8 Arabicpython2.7

    import matplotlib.pyplot as plt
    labels = ['عشرة','عشرين','ثلاثين']
    #labels=['A','B','C']
    x = [10, 12,13]
    y = [10,20,30]
    plt.figure(figsize=(16, 16)) 
    for i in range(len(x)):
          plt.scatter(x[i],y[i])
          plt.annotate(labels[i],
                 xy=(x[i], y[i]),
                 xytext=(5, 2),
                 textcoords='offset points',
                 ha='right',
                 va='bottom')
    plt.show()

for utf-8(labels = ['عشرة','عشرين','ثلاثين'])我有错误

  

UnicodeDecodeError:'ascii'编解码器无法解码字节0xd8   0:序数不在范围内(128)

虽然标签= ['A','B','C']时工作正常。

1 个答案:

答案 0 :(得分:0)

我解决了我的阿拉伯字体问题。如果有人需要,我想分享解决方案。

0-确保你的matplotlib是2.2版

检查

  

导入matplotlib

     

matplotlib。的版本

     

'0.98.0'%如果是这种情况,那么你需要升级它

升级

  

sudo pip install matplotlib --upgrade

1-安装bidi

  

sudo easy_install python-bidi

2-安装arabic_reshaper,使字母顺序从右边开始,让

  

sudo pip install arabic_reshaper

3 - 按以下方式更改您的代码

    #!/usr/bin/python2.7
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    import arabic_reshaper
    from bidi.algorithm import get_display
    import matplotlib.pyplot as plt
    labels = ['عشرة','عشرين','ثلاثين']
    x = [10, 12,13]
    y = [10,20,30]
    plt.figure(figsize=(16, 16)) 
    for i in range(len(x)):
          a=arabic_reshaper.reshape(labels[i])
          aa = get_display(a)
          plt.scatter(x[i],y[i])
          plt.annotate(aa,
                  xy=(x[i], y[i]),
                  xytext=(5, 2),
                  textcoords='offset points',
                  ha='right',
                  va='bottom')
    plt.show()

欲了解更多信息, https://matplotlib.org/faq/troubleshooting_faq.html

https://matplotlib.org/users/dflt_style_changes.html?highlight=changes%20default%20style

Print Arabic words from Python to ESC/POS printer?

http://mpcabd.xyz/python-arabic-text-reshaper/

谢谢