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

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

问题描述

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

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

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

在 Chronial 建议选择合适的字体后,我在 python33 shell 中执行了这些命令:

<预><代码>>>>导入 matplotlib.pyplot>>>matplotlib.rcParams.update({font.family': 'TraditionalArabic'})

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

编辑#3:在系统中安装阿拉伯字体后,我可以用阿拉伯文字生成图形,但文字是从左到右显示的.朝着最后阶段的良好进展:这是从右到左出现的阿拉伯文字.下面是图表截图:

你的,

穆罕默德

解决方案

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

from bidi.algorithm import get_display导入 matplotlib.pyplot 作为 plt导入 arabic_reshaper将 networkx 导入为 nx# 阿拉伯语文本预处理reshape_text = arabic_reshaper.reshape(u'لغةٌ عربيّة')arttext = get_display(reshape_text)# 构建示例图G=nx.Graph()G.add_edge('a', arttext ,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('关闭')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天全站免登陆