1

This is a followup question to this question. Since it addresses a more general issue I make it a new question.

I have a network for which the labels of the nodes are in Farsi language (Arabic alphabet). When I try to use networkx to display my network it shows blank squares instead of Arabic letters. Below I copy a good example provided in the answers in here.

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

which generates the following image:

enter image description here

I tried to install the needed fonts by following command lines in python, but I get the same thing.

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

Here is the ERROR message, to be more specific:

/usr/local/anaconda3/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['TraditionalArabic'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext])

I am also investigating ways to install the needed fonts from ubuntu cli, if possible, and put it in my docker file as it gets installed every time I spin my runs.

Best regards, s.

Rotail
  • 1,025
  • 4
  • 19
  • 40
  • Is the font `TraditionalArabic` installed? – Ashok Arora Mar 29 '18 at 18:51
  • I'm pretty sure no. I am not sure how can I install a font from ubuntu cli. – Rotail Mar 29 '18 at 19:00
  • I think you can download the font here and install on ubuntu: https://fonts2u.com/traditional-arabic.font – TYZ Mar 29 '18 at 20:43
  • There's an arabic font at https://github.com/googlei18n/noto-fonts/blob/master/unhinted/NotoNaskhArabic-Regular.ttf , download it and place it in your .fonts directory in ubuntu. – Ashok Arora Mar 29 '18 at 22:29
  • Salaam, This is almost answered here: [use-matplotlib-pyplot-rcparams-with-a-custom-font-which-is-not-installed](https://stackoverflow.com/questions/47757755/use-matplotlib-pyplot-rcparams-with-a-custom-font-which-is-not-installed) – Abolfazl Danayi Dec 30 '21 at 15:42

0 Answers0