Python Networkx和matplotlib中的从右到左支持 [英] Right-to-left Support in Python Networkx and matplotlib

查看:199
本文介绍了Python Networkx和matplotlib中的从右到左支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





这里是用阿拉伯文字写成的阿拉伯语单词的简图(从右到左写)。





现在的问题是:如何在使用python networkx和matplotlib.pyplot生成的图中显示阿拉伯语脚本?编辑:在Chronial建议选择合适的字体后,我在python33 shell中执行了这些命令:

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

然后我构建了与阿拉伯词汇的图表。但是,绘制图表并未显示阿拉伯文字。它显示出正方形。我不知道matplotlib.pyplot是使用系统字体还是具有自己的字体包。假设matplotlib.pyplot使用系统字体,那么它应该显示阿拉伯语脚本。似乎阿拉伯语字体需要安装到matplotlib.pyplot。但我不知道该怎么做。

编辑#3:
在系统中安装阿拉伯语字体之后,我可以用阿拉伯语脚本生成图表,但脚本从左侧出现-to-权。迈向最后阶段的良好进展:这是从右至左出现的阿拉伯文字体。下面是该图的一个镜头:



您的,

穆罕默德

p>

解决方案

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

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

#阿拉伯文本预处理
reshaped_text = arabic_reshaper.reshape(u'لغةعربية')
artext = get_display(reshaped_text)

#构造样本图
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)

#绘制阿拉伯文字
#字体Times New Roman中有阿拉伯文。
#你可以在这里使用任何阿拉伯字体。
nx.draw_networkx_labels(G,pos,font_size = 20,font_family ='Times New Roman')

#显示图形
plt.axis('off')
plt.show()


I have tried to draw lexicographic graphs with python33 networkx and matplotlib running on Linux Fedora 19 KDE, 64 bits. When feeding English script as input data, the graphs are drawn well. However, when providing Arabic script as input data, all I get is squares queued in juxtaposition. This is an example of a simple graph in English script:

and here is a simple graph of Arabic words written in Arabic script, (which is written from Right-to-left).

The question is: how can I show Arabic script in the graphs that I generate using python networkx and matplotlib.pyplot? I really appreciate your kind help!

Edit: after Chronial suggested selecting the the proper font, I executed these commands in the python33 shell:

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

Then I constructed the graph with Arabic words. However, drawing the graph did not show Arabic script. It showed jsut squares. I do not know whether the matplotlib.pyplot uses the system fonts or it has its own font packages. Assuming that the matplotlib.pyplot uses the system font, then it should have shown Arabic scripts. It seems that Arabic fonts needs to be installed to the matplotlib.pyplot. But I don't know how to do that. Your help is highly appreciated!

Edit # 3: After installing Arabic fonts into the system, I could generate graphs with Arabic script but the script appears from left-to-right. A good progress towards the final stage: which is Arabic script appearing from Right to left. Below is a shot of the graph:

Yours,

Mohammed

解决方案

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

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

这篇关于Python Networkx和matplotlib中的从右到左支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆