#321 Custom NetworkX graph appearance

The chart #320 explain how to realise a basic network chart. Now, let’s have a look to the arguments that allows to custom the appearance of the chart. The customisations are separated in 3 main categories: nodes, node labels and edges:

  •  

     

     

     

     

     

    You can easily control the nodes with the few arguments described below. Note that the available shapes are described here. Note that linewidths allow to control the width of the border of your shape.

     

     

     

     

     

    
    # libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    
    # Build a dataframe with your connections
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']}) 
    df
    
    # Build your graph
    G=nx.from_pandas_dataframe(df, 'from', 'to')
    
    # Graph with Custom nodes:
    nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", node_shape="s", alpha=0.5, linewidths=40)
    plt.show()
    
    
  •  

     

     

     

     

     

    Then you can custom the labels written in each node. Don’t forget to add with_labels=True

    to show the labels.

     

     

     

     

     

    
    # libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    
    # Build a dataframe with your connections
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']}) 
    df
    
    # Build your graph
    G=nx.from_pandas_dataframe(df, 'from', 'to')
    
    # Custom the edges:
    nx.draw(G, with_labels=True, node_size=1500, font_size=25, font_color="yellow", font_weight="bold")
    
    
  •  

     

     

     

    Of course you can modify edges as well. You can use different types of line using the style argument. See here to see the different possibilities.

     

     

     

     

     

     

     

    
    # libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    
    # Build a dataframe with your connections
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
    df
    
    # Build your graph
    G=nx.from_pandas_dataframe(df, 'from', 'to')
    
    # Chart with Custom edges:
    nx.draw(G, with_labels=True, width=5, edge_color="skyblue", style="solid")
    
    
  •  

     

     

     

     

    We can of course custom nodes, edges and label in the same time!

     

     

     

     

     

     

    
    # libraries
    import pandas as pd
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    
    # Build a dataframe with your connections
    df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
    df
    
    # Build your graph
    G=nx.from_pandas_dataframe(df, 'from', 'to')
    
    # All together we can do something fancy
    nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", node_shape="o", alpha=0.5, linewidths=4, font_size=25, font_color="grey", font_weight="bold", width=2, edge_color="grey")
    
    

 

Leave a Reply

Your email address will not be published.