9

xlabel and ylabel don't display Arabic

x = [2, 4, 6, 8, 10]
y = [6, 7, 8, 9, 10]

plt.bar(x, y, label='Bar1', color='red')
plt.xlabel("الفواصل")
plt.ylabel("الترتيبات")
plt.show()

enter image description here

STF
  • 1,485
  • 3
  • 19
  • 36
Ahmed007
  • 201
  • 1
  • 3
  • 5

2 Answers2

10

You will first need to install both arabic-reshaper and python-bidi.

import arabic_reshaper
from bidi.algorithm import get_display
import matplotlib.pyplot as plt
x = [2, 4, 6, 8, 10]
y = [6, 7, 8, 9, 10]
xlbl = get_display( arabic_reshaper.reshape('الفواصل'.decode('utf8')))
ylbl = get_display( arabic_reshaper.reshape('الترتيبات'.decode('utf8')))
plt.bar(x, y, label='Bar1', color='red')
plt.xlabel(xlbl, fontdict=None, labelpad=None)
plt.ylabel(ylbl, fontdict=None, labelpad=None)
plt.show()

plot Arabic

Ali Hassaine
  • 579
  • 5
  • 19
-5

It should work if you use unicode strings (u"الفواصل") instead of usual strings.

x = [2, 4, 6, 8, 10]
y = [6, 7, 8, 9, 10]

plt.bar(x, y, label='Bar1', color='red')
plt.xlabel(u"الفواصل")
plt.ylabel(u"الترتيبات")
plt.show()

enter image description here

Or, in order to get the correct order

import matplotlib.pyplot as plt
x = [2, 4, 6, 8, 10]
y = [6, 7, 8, 9, 10]

plt.bar(x, y, label='Bar1', color='red')
plt.xlabel(u"الفواصل"[::-1])
plt.ylabel(u"الترتيبات"[::-1])
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 1
    I still have the same problem – Ahmed007 Nov 01 '17 at 15:19
  • 1
    In that case, you might need to update matplotlib. It might be that your version is too old and uses a font that does not have all unicode symbols available. – ImportanceOfBeingErnest Nov 01 '17 at 15:21
  • Yes, it should work, if the font actually has those Arabic glyphs. And the `u` prefix is only required in Python 2, in Python 3 all normal text strings are Unicode objects, so the `u` prefix isn't required (although recent Python 3 versions permit it for reasons of Python 2 compatibility). – PM 2Ring Nov 01 '17 at 15:50
  • BTW, there's still a big problem with this: the letters are in the wrong order (left-to-right) and disjoint. – halflings Dec 05 '17 at 05:34
  • I don't know how to connect the letters. But you may reverse the string for correct ordering. – ImportanceOfBeingErnest Dec 05 '17 at 09:50