Python Networkx和matplotlib中从右到左的支持

Moh*_*med 6 python graph matplotlib arabic networkx

我试图用python33 networkx和matplotlib在Linux Fedora 19 KDE(64位)上运行绘制字典图.在输入英文脚本作为输入数据时,图形绘制得很好.但是,当提供阿拉伯语脚本作为输入数据时,我得到的是并列排队的正方形.这是英文脚本中简单图形的示例:

用英文字母写的阿拉伯文字

这是一个用阿拉伯文字写的阿拉伯语单词的简单图表(从右到左书写).

用阿拉伯字母写的阿拉伯文字

问题是:如何在使用python networkx和matplotlib.pyplot生成的图形中显示阿拉伯语脚本?非常感谢您的帮助!

编辑:在Chronial建议选择正确的字体后,我在python33 shell中执行了这些命令:

>>> import matplotlib.pyplot
>>> matplotlib.rcParams.update({font.family' : 'TraditionalArabic'})
Run Code Online (Sandbox Code Playgroud)

然后我用阿拉伯语构建了图形.但是,绘制图形时未显示阿拉伯文字.它显示了jsut正方形.我不知道matplotlib.pyplot是使用系统字体还是它有自己的字体包.假设matplotlib.pyplot使用系统字体,那么它应该显示阿拉伯语脚本.似乎需要将阿拉伯字体安装到matplotlib.pyplot.但我不知道该怎么做.非常感谢您的帮助!

编辑#3:在系统中安装阿拉伯字体后,我可以使用阿拉伯语脚本生成图表,但脚本从左到右显示.在最后阶段取得了很好的进展:这是从右到左出现的阿拉伯文字.下面是图表的一个镜头:

阿拉伯文字,但从左到右而不是从右到左出现

此致,

穆罕默德

Nas*_*ibi 5

对于matplotlib 中的阿拉伯语,您需要bidi.algorithm.get_displayarabic_reshaper模块:

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

# Arabic text preprocessing 
reshaped_text = arabic_reshaper.reshape(u'???? ??????')
artext = get_display(reshaped_text)

# constructing the sample graph
G=nx.Graph()
G.add_edge('a', artext ,weight=0.6)
pos=nx.spring_layout(G) 
nx.draw_networkx_nodes(G,pos,node_size=700)
nx.draw_networkx_edges(G,pos,edgelist=G.edges(data=True),width=6)

# Drawing Arabic text
# Just Make sure your version of the font 'Times New Roman' has Arabic in it. 
# You can use any Arabic font here.
nx.draw_networkx_labels(G,pos,font_size=20,font_family='Times New Roman')

# showing the graph
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明