Matplotlib:从右到左文本(希伯来语、阿拉伯语等)

2024-10-01 17:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的图中添加一些文本,这是RTL(在这个例子中,希伯来语)。在做了一些工作后,设法让它显示文本,但它显示的是LTR(意思是,按reverese顺序)。我查了一下参考资料,在网上做了大量的搜索,结果什么也没找到。在

我使用的示例:

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

显示“אהתיכםלוש”。 如果你看不到希伯来语,就好像我输入了“Hello”,而输出将是“olleH”。在

我不能简单地反转输入,因为它是LTR和RTL的混合。在

我们将非常感谢您的帮助。在


Tags: text文本import示例顺序matplotlibasplt
3条回答

无论谁遇到同样的问题,我都找到了一个部分解决办法。在

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

正确显示。在

那为什么它是部分的呢? 因为我发现bidi包有时会弄乱我与matplotlib一起使用的latex表达式。所以要小心使用。在

对于阿拉伯语您需要bidi.algorithm.get_display和{a1}模块:

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

我也有同样的问题,我认为使用@Korem和@Nasser Al-Wohaibi的两个答案:

import arabic_reshaper
from bidi.algorithm import get_display

new_text=get_display(arabic_reshaper.reshape(old_text))

因为只有阿拉伯文的整形器没有重新排列字母,而bidi只是没有把它们组合起来

^ ^

相关问题 更多 >

    热门问题