NetworkX is a Python library for studying graphs and networks. NetworkX is free software released under the BSD-new license. It can be used to create and manipulate complex networks, and to study the structure and function of complex networks.

With NetworkX, you can load or store networks in standard or non-standard data formats. It can generate many types of random or classic networks, analyze network structure, build network models, design new network algorithms, and draw networks.

Of course, NetworkX alone cannot be powerful. Here, Chunjian Muke will use other widely used common Python libraries to draw various basic network graphs.


1. Drawing the Most Basic Network Graph

A network graph consists of nodes and edges. In NetworkX, each row of a pandas DataFrame represents the points in a connection, and a connection is generated at the corresponding position. In the example, a connection is generated between each corresponding position of ‘from’ and ’to’.

################import required libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt

################Draw network graph

    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
    basic_net=nx.from_pandas_edgelist(df, 'from', 'to')
    fig = plt.figure()
    nx.draw(basic_net, with_labels=True)
    plt.show()

Drawing NetworkX Network Graphs in python3

Drawing a network graph using NetworkX only requires the simple steps above.

1. Importing Data

basic_net=nx.from_pandas_edgelist(df, 'from', 'to'). Of course, NetworkX supports importing various data formats. For details on nx.from_pandas_adjacency, please refer to https://networkx.github.io/documentation/latest/reference/convert.html

2. Drawing the Graph

nx.draw(basic_net, with_labels=True)

3. Display

plt.show()


2. Differentiating Connection Colors and Sizes Between Nodes

Since the importance of connections between different nodes varies, using line segments or colors of different sizes for connections becomes extremely important. NetworkX conveniently provides this function; you just need to specify edge_color during the drawing process.

################import required libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C'], 'value':[1, 10, 5, 5]})

################Draw network graph
    color_net=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.Graph() )
    ###Specify color and size
    fig = plt.figure()
    nx.draw(color_net, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['value'], width=10.0)
    plt.show()

Drawing NetworkX Network Graphs in python3 color_net


3. Setting Network Graph Background Color

In NetworkX, you can set the background color simply by using set_facecolor.

################import required libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C'], 'value':[1, 10, 5, 5]})

################Draw network graph
    color_net=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.Graph() )
    ###Specify color and size
    fig = plt.figure()
    nx.draw(color_net, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['value'], width=10.0)
    fig.set_facecolor("#AAAA00")###Add only here
    plt.show()

Drawing NetworkX Network Graphs in python3 color_back_net


Summary

This article provides a basic introduction to using the NetworkX package for drawing network graphs. If you’re wondering how to draw network graphs in Python, NetworkX is the way to go. For detailed official documentation, please refer to NetworkX Official Documentation.