Giter Club home page Giter Club logo

dsc-networkx-intro's Introduction

Introduction to NetworkX

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 with networkx
  • Add nodes and edges to networkx graphs
  • 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 the nx.draw() function 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=0.7, font_weight='bold')

png

Additional Resources

Summary

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

dsc-networkx-intro's People

Contributors

sumedh10 avatar mathymitchell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.