7

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:

Arabic words written with English letters

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

Arabic words written in Arabic letters

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:

Arabic script, but appearing from Left to right instead of Right-to-left

Yours,

Mohammed

Mohammed
  • 1,364
  • 5
  • 16
  • 32
  • Are you sure that you have selected a font that contains those characters? – Chronial Sep 12 '13 at 22:58
  • @Chronial: No, I did not select a font that contain Arabic script, simply because I don't know how to do that. Could you please show me how to do it. If you know any tutorial on selecting fonts and formatting the graphs, please suggest it to me. I really appreciate your kind help! – Mohammed Sep 13 '13 at 03:44
  • You should be able to find all the required information here: http://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot – Chronial Sep 13 '13 at 03:50
  • @Chronial, Thank you for your help. Could you show me how to install the family font 'traditional arabic' into python matplotlib.pyplot? Appreciating your kind help! – Mohammed Sep 13 '13 at 04:27
  • Did the link I posted not help you? – Chronial Sep 13 '13 at 04:50
  • @Chronial, Thank you for your kind help. I followed the post you provided and Python accepted the commands, but it did not show the Arabic script. Please see the edit I incorporated to the main post above. – Mohammed Sep 13 '13 at 17:37
  • Did that font change work? If it did, western characters should also look different now. And Matplotlib uses the system fonts. – Chronial Sep 13 '13 at 22:02
  • @Chronial, after several days of struggling with Arabic fonts, I could generate graphs with Arabic node lables, but the problem is that "Arabic characters appear in the graph from Left to right, same as English, not from Right to left. You can see them in the picture above which I just incorporated. Meanwhile, your suggestions were of great help to me. I will continue reading the manual and try to find support for Arabic in the matplotlib and networkx. – Mohammed Sep 21 '13 at 22:04
  • 1
    You could try to put [the direction reverse character](http://www.fileformat.info/info/unicode/char/202e/index.htm) at the beginning of your strings. You can generate it in python with `u"\u202C"`. – Chronial Sep 22 '13 at 01:48
  • @Mohammed can you please share how did you install arabic words from terminal? Thank you – Rotail Mar 29 '18 at 17:20
  • @Rotail Please see this post and follow the instructions there. There is a package that you should install. After that, you can launch it from terminal and you can type from right to left. https://askubuntu.com/questions/77657/how-to-enable-arabic-support-in-gnome-terminal – Mohammed Mar 30 '18 at 18:57

1 Answers1

5

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

enter image description here

Nasser Al-Wohaibi
  • 4,562
  • 2
  • 36
  • 28
  • Thank you, Nasser, for your help. Unfortunately, I could not install arabic_reshaper on Linux. It throws installation error. Further, Python bidi supports just Python 2.7 which has not native UTF-8 support. – Mohammed Jan 04 '15 at 10:12
  • Here's error Downloading/unpacking https://github.com/mpcabd/python-arabic-reshaper/archive/master.zip Downloading master.zip Running setup.py (path:/tmp/pip-umSNFY-build/setup.py) egg_info for package from https://github.com/mpcabd/python-arabic-reshaper/archive/master.zip Traceback (most recent call last): File "", line 17, in IOError: [Errno 2] No such file or directory: '/tmp/pip-umSNFY-build/setup.py' Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 17, in – Mohammed Jan 04 '15 at 10:13
  • arabic_reshaper is a simple python module, no need to run setup.py since it hasn't any. Just copy and paste arabic_reshaper.py to python's PythonXXX\Lib\site-packages\ and you are good to go. – Nasser Al-Wohaibi Jan 04 '15 at 16:27
  • /usr/local/anaconda3/lib/python3.5/site-```packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))``` I am not sure how can I install a font in terminal – Rotail Mar 29 '18 at 15:46