Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right-to-left Support in Python Networkx and matplotlib

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

like image 945
Mohammed Avatar asked Sep 12 '13 19:09

Mohammed


People also ask

For which purpose matplotlib library is used in Python language?

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

What is matplotlib used for in machine learning?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.

Which one is recommended way to load matplotlib library is?

Recommended way to load matplotlib library is install matplotlib. first of all Go to the terminal window and then type pip install matplotlib and press enter.

What is Alpha argument in matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.


1 Answers

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

like image 132
Nasser Al-Wohaibi Avatar answered Oct 02 '22 12:10

Nasser Al-Wohaibi