Right-to-left Support in Python Networkx and matplotlib

后端 未结 1 1153
忘掉有多难
忘掉有多难 2020-12-19 06:38

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 graph

相关标签:
1条回答
  • 2020-12-19 07:24

    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

    0 讨论(0)
提交回复
热议问题