Matplotlib: Writing right-to-left text (Hebrew, Arabic, etc.)

For whoever encounters the same problem, I found a partial solution.

The bidi package provides this functionality, so using:

from bidi import algorithm as bidialg
import matplotlib.pyplot as plt
text = bidialg.get_display(u'שלום כיתה א')
plt.text(0.5, 0.5, text , name = 'Arial')
plt.show()

displays it correctly.

So why is it partial? Because I found out that the bidi package sometimes messes up latex expression which I use with matplotlib. So use it carefully.


I had the same issue and i think that using both answers of @Korem and @Nasser Al-Wohaibi like:

import arabic_reshaper
from bidi.algorithm import get_display

new_text=get_display(arabic_reshaper.reshape(old_text))

because only the arabic_reshaper didnt rearrange the letters and the bidi only didn't combine them

^_^


For Arabic you need both bidi.algorithm.get_display and arabic_reshaper modules:

from bidi.algorithm import get_display
import matplotlib.pyplot as plt
import arabic_reshaper

reshaped_text = arabic_reshaper.reshape(u'لغةٌ عربيّة')
artext = get_display(reshaped_text)

plt.text(0.25, 0.45, artext , name = 'Times New Roman',fontsize=50)
plt.show()

python matplotlib arabic text