NetworkX: Drawing Graphs














































NetworkX: Drawing Graphs



Python: NetworkX

NetworkX: Drawing Graphs

Drawing NetworkX graphs is important because it helps summarize information and graph/network structure and properties in a manner which is easy to understand and detailed.

NetworkX is not primarily a graph drawing package but basic drawing with Matplotlib as well as an interface to use the open source Graphviz software package are included. These are part of the 'networkx.drawing' module.

For creating and drawing graphs we will use Jupyter notebook.

To install Jupyter notebook enter the following command in a Terminal or Command Line:

    pip3 install notebook

To check whether Jupyter notebook is installed or not use the following command:

    pip3 show notebook

To open Jupyter notebook use the following command:

    python3 -m notebook

Create a new Python3 notebook as shown in the image below.


In the Jupyter notebook, import the networkx module.

import networkx as nx

Create a sample graph.

G = nx.petersen_graph()

Now, we will draw this graph in different ways:

  1. Using the draw() method.

nx.draw(G)

The output looks like this.


  1. Using the draw() method with labels.

nx.draw(G, with_labels=True, font_weight='bold')

The output looks like this.


  1. Using the draw_shell() method with labels.

nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')

The output looks like this.




More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1026 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1672 2
nose: Working 506 2
pytest: Working 510 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 663 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 508 2
NetworkX: Multigraphs 648 2
Tracking User Activity with CherryPy 1396 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1301 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 483 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 607 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 612 2
Top 5 Free Online IDEs of 2020 866 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 581 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2278 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments