加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:36:02 所属栏目:Python 来源:网络整理
导读:我试图用 python33 networkx和matplotlib在 Linux Fedora 19 KDE(64位)上运行绘制字典图.在输入英文脚本作为输入数据时,图形绘制得很好.但是,当提供阿拉伯语脚本作为输入数据时,我得到的是并列排队的正方形.这是英文脚本中简单图形的示例: 这是一个用阿拉伯
我试图用 python33 networkx和matplotlib在 Linux Fedora 19 KDE(64位)上运行绘制字典图.在输入英文脚本作为输入数据时,图形绘制得很好.但是,当提供阿拉伯语脚本作为输入数据时,我得到的是并列排队的正方形.这是英文脚本中简单图形的示例:

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

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

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

>>> import matplotlib.pyplot
>>> matplotlib.rcParams.update({font.family' : 'TraditionalArabic'})

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

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

此致,

穆罕默德

解决方法

对于matplotlib中的阿拉伯语,您需要bidi.algorithm.get_display和 arabic_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,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,font_size=20,font_family='Times New Roman')

# showing the graph
plt.axis('off')
plt.show()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读