2013-03-14 184 views
9

我試圖添加一些文本到我的情節是RTL(在這種情況下,希伯來語)。在一些工作設法讓它顯示文本之後,它顯示了LTR(意思是,按照順序排列)。我已經挖掘了參考資料,並在網上進行了廣泛的搜索,沒有出現。Matplotlib:編寫從右到左的文本(希伯來語,阿拉伯語等)

一個例子,我使用的是什麼:

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

並顯示 'אהתיכםלוש'。 如果你看不到希伯來語,就好像我輸入'你好',輸出將是'olleH'。

我不能簡單地反轉輸入,因爲它是混合LTR和RTL。

每一個幫助將不勝感激。

+0

你可以有RTL和LTR '字' 分開?那麼你可以在將它們全部連接在一起之前將RTL轉換爲 – 2013-03-14 23:24:21

+0

在字符串中是否有明確的unicode方向字符? – tacaswell 2013-03-15 03:06:39

+0

@振亞 - 不,我不能。它們提供給我的代碼,而不是由它組裝。 – Korem 2013-03-16 12:10:23

回答

12

對於遇到同樣問題的人,我找到了部分解決方案。

bidi package提供了此功能,因此,使用:正確

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() 

顯示它。

那麼,它爲什麼是部分? 因爲我發現雙向包裝有時會混淆與matplotlib一起使用的乳膠表達。所以仔細使用它。

10

對於阿拉伯語你既需要bidi.algorithm.get_displayarabic_reshaper模塊:

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