Thanks to visit codestin.com
Credit goes to github.com

Skip to content

LukaMrPython/dsc-networkX-intro-houston-ds-042219

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetworkX Introduction

Introduction

The primary package for analyzing network graphs in Python is NetworkX. In this lesson you'll get a brief introduction to the package, recreating the basic graphs from the previous lesson by adding nodes and edges and then creating a visual.

Objectives

You will be able to:

  • Create basic network graphs using NetworkX
  • Add nodes to network graphs with NetworkX
  • Add edges to network graphs with NetworkX
  • Visualize network graphs with NetworkX

Creating a Graph

Creating the initial graph is incredible simple. Observe:

import networkx as nx
G = nx.Graph()

Adding Nodes

From there, adding nodes is just as easy. Simply call the add_node method from you graph instance.

G.add_node("Bob")

Of course, you can also combine this with some of your previous Python prowess!

people = ["Sally", "Kate", "Jen", "Jake", "Doug"]
for person in people:
    G.add_node(person)

Adding Edges

Similarly, adding edges is also quite straightforward.

G.add_edge("Bob", "Sally")

Once again, you can also take advantage of your knowledge of python data structures to create a nested data structure and then feed these pairs into the add_edge method.

relations = {"Bob": ["Jen", "Kate"],
            "Jen": ["Bob", "Sally", "Jake", "Doug", "Kate"],
            "Doug": ["Bob"]
            }
for p1 in relations.keys():
    p2s = relations[p1]
    for p2 in p2s:
        G.add_edge(p1, p2)

Visualizing the Graph

Finally, let's take a look at visualizing this graph! The only required parameter to nx.draw() is specifying the graph itself. In addition, demonstrated below are a number of optional parameters:

  • with_labels (boolean) - would you like labels for your nodes?
  • node_color (color) - what color do you want your nodes?
  • node_size (real) - how big do you want your nodes? (300 is default)
  • alpha (real) - node transparency, must be between 0 and 1, 1 being the default
  • font_weight (string) - additional formatting for the label text
%matplotlib inline
nx.draw(G, with_labels=True, node_color="#1cf0c7", node_size=1500, alpha=.7, font_weight="bold")

png

Additional Resources

Summary

Well done! In this lab, you got a brief introduction to using NetworkX to create and visualize graph networks. In the upcoming lab, you'll get a chance to further practice these skills before moving on to common algorithms and metrics for processing and interpreting network graphs.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 100.0%