#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")
    
    

 

  • Sponsors

  • 2 comments

    • The following error occurs:

      C:\Users\Anuvab Nandi\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:126: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
      Future behavior will be consistent with the long-time default:
      plot commands add elements without first clearing the
      Axes and/or Figure.
      b = plt.ishold()
      C:\Users\Anuvab Nandi\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:138: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
      Future behavior will be consistent with the long-time default:
      plot commands add elements without first clearing the
      Axes and/or Figure.
      plt.hold(b)
      C:\Users\Anuvab Nandi\Anaconda3\lib\site-packages\matplotlib\__init__.py:917: UserWarning: axes.hold is deprecated. Please remove it from your matplotlibrc and/or style files.
      warnings.warn(self.msg_depr_set % key)
      C:\Users\Anuvab Nandi\Anaconda3\lib\site-packages\matplotlib\rcsetup.py:152: UserWarning: axes.hold is deprecated, will be removed in 3.0
      warnings.warn(“axes.hold is deprecated, will be removed in 3.0”)

      Reply

    Leave a Reply to Humberto Kramm Cancel reply

    Your email address will not be published.