By default, networkx has problems with drawing self-loops in graphs.

Overview

networkx drawing self-loops

By default, networkx has problems with drawing self-loops in graphs. It makes it hard to draw a graph with self-loops or to make a nicely looking chord diagram. This repository provides some code to draw self-loops nicely

Examples

Let's have a look on how networkx draws graphs with self-loops

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np 

from drawing import chord_diagram  # Code from this repo

# Create graph with some weights
graph = nx.DiGraph(
    np.array([
        [1, 2, 1, 3, 5],
        [1, 0, 3, 0, 0],
        [1, 1, 3, 0, 1],
        [0, 0, 2, 0, 1],
        [1, 1, 1, 1, 1]
    ])
)

fig, ax = plt.subplots(figsize=(10, 10))

nx.draw_circular(graph, ax=ax)
ax.set_title("Graph drawn with networkx")

fig.savefig("images/0_simple_graph_networkx.png")

Simple graph networkx

Self-loops are drawn, but they are pretty ugly. When the graph gets bigger, it is not possible to see them anymore, especially at the inner side of the polygon. This repository provides a code for the much nicer plots:

fig, ax = plt.subplots(figsize=(10, 10))

ax = chord_diagram(graph, ax)
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_title("A simple graph drawn better")

fig.savefig("images/1_simple_graph.png")

Simple graph

Much better, huh? Let's have a look at the real world example. This is cells communication network from single-cell RNA-seq data. What you get by vanilla networkx:

Cells communication networkx

And by code from this repo:

Cells communication

Of course, you can make networkx plots with colors and arc curvy edges too. But notice, how vanilla networkx draws self-loops. They always have the same directions. While the code from this repo draws them from the center of the layout. More than that, it is possible to control self-loops' width and length. And, if necessary, draw them inside the polygon. Check documentation of the function draw_self_loop() from drawing.py

You can see more code examples in example.py

What can be done better

This repository can become better. If people use it, I will definitely continue developing. If you want to help me, PRs and suggestions are always welcome!

If you want to contribute, here are some ideas of what can be done better:

  • Make code more flexible. In the current version, a lot of things (e. g. plot colors, edges curvature) are hardcoded inside the functions from drawing.py. It would be great to make them parameters of the functions.
  • Make chord_diagram a PyPi package. It would be great to do a simple pip install package_name instead of copy-pasting code from this repo
  • Write some tests. It is not simple to automatically test plots, as they are mostly evaluated visually. But it is definitely a good idea to test some math related functions from drawing.py.
  • Nicely draw self-loops in a complicated layout. For now, self-loops look nicely if the graph has circular layout centered around (0, 0). In more complicated graphs, you will most likely get ugly plots. For example, this is a graph with Kamada-Kawai layout: Not circular layout

You can see at least 2 problems on this plot:

  1. Loops get bigger as they get further away from the center. This is an expected outcome from the current version of the code. But maybe we want to make it constant
  2. There are some weird extra self-loops. Tbh, I am not sure, where do they come from

Installation

  1. Install requirements:

pip install -r requirements.txt

Note that you need pandas only to run some examples. If you want to use only the code for the chord diagram, pandas is not necessary.

  1. Use code from drawing.py or check examples.py to see some code examples
You might also like...
 Some problems of SSLC ( High School ) before outputs and after outputs
Some problems of SSLC ( High School ) before outputs and after outputs

Some problems of SSLC ( High School ) before outputs and after outputs 1] A Python program and its output (output1) while running the program is given

Python code for solving 3D structural problems using the finite element method
Python code for solving 3D structural problems using the finite element method

3DFEM Python 3D finite element code This python code allows for solving 3D structural problems using the finite element method. New features will be a

BGraph is a tool designed to generate dependencies graphs from Android.bp soong files.

BGraph BGraph is a tool designed to generate dependencies graphs from Android.bp soong files. Overview BGraph (for Build-Graphs) is a project aimed at

Personal IMDB Graphs with Bokeh
Personal IMDB Graphs with Bokeh

Personal IMDB Graphs with Bokeh Do you like watching movies and also rate all of them in IMDB? Would you like to look at your IMDB stats based on your

A D3.js plugin that produces flame graphs from hierarchical data.
A D3.js plugin that produces flame graphs from hierarchical data.

d3-flame-graph A D3.js plugin that produces flame graphs from hierarchical data. If you don't know what flame graphs are, check Brendan Gregg's post.

Here are my graphs for hw_02
Here are my graphs for hw_02

Let's Have A Look At Some Graphs! Graph 1: State Mentions in Congressperson's Tweets on 10/01/2017 The graph below uses this data set to demonstrate h

Generate knowledge graphs with interesting geometries, like lattices
Generate knowledge graphs with interesting geometries, like lattices

Geometric Graphs Generate knowledge graphs with interesting geometries, like lattices. Works on Python 3.9+ because it uses cool new features. Get out

Python ts2vg package provides high-performance algorithm implementations to build visibility graphs from time series data.
Python ts2vg package provides high-performance algorithm implementations to build visibility graphs from time series data.

ts2vg: Time series to visibility graphs The Python ts2vg package provides high-performance algorithm implementations to build visibility graphs from t

 Flame Graphs visualize profiled code
Flame Graphs visualize profiled code

Flame Graphs visualize profiled code

Owner
Vladimir Shitov
I am a student at the Siberian State Medical University in Tomsk, Russia. I enjoy bioinformatics, programming and data analysis.
Vladimir Shitov
Draw interactive NetworkX graphs with Altair

nx_altair Draw NetworkX graphs with Altair nx_altair offers a similar draw API to NetworkX but returns Altair Charts instead. If you'd like to contrib

Zachary Sailer 156 Feb 6, 2021
Generate graphs with NetworkX, natively visualize with D3.js and pywebview

webview_d3 This is some PoC code to render graphs created with NetworkX natively using D3.js and pywebview. The main benifit of this approac

byt3bl33d3r 68 Aug 18, 2022
A high-level plotting API for pandas, dask, xarray, and networkx built on HoloViews

hvPlot A high-level plotting API for the PyData ecosystem built on HoloViews. Build Status Coverage Latest dev release Latest release Docs What is it?

HoloViz 697 Jan 6, 2023
A high-level plotting API for pandas, dask, xarray, and networkx built on HoloViews

hvPlot A high-level plotting API for the PyData ecosystem built on HoloViews. Build Status Coverage Latest dev release Latest release Docs What is it?

HoloViz 349 Feb 15, 2021
Fastest Gephi's ForceAtlas2 graph layout algorithm implemented for Python and NetworkX

ForceAtlas2 for Python A port of Gephi's Force Atlas 2 layout algorithm to Python 2 and Python 3 (with a wrapper for NetworkX and igraph). This is the

Bhargav Chippada 227 Jan 5, 2023
erdantic is a simple tool for drawing entity relationship diagrams (ERDs) for Python data model classes

erdantic is a simple tool for drawing entity relationship diagrams (ERDs) for Python data model classes. Diagrams are rendered using the venerable Graphviz library.

DrivenData 129 Jan 4, 2023
Python module for drawing and rendering beautiful atoms and molecules using Blender.

Batoms is a Python package for editing and rendering atoms and molecules objects using blender. A Python interface that allows for automating workflows.

Xing Wang 1 Jul 6, 2022
This is simply repo for line drawing rendering using freestyle in Blender.

blender_freestyle_line_drawing This is simply repo for line drawing rendering using freestyle in Blender. how to use blender2935 --background --python

MaxLin 3 Jul 2, 2022
This is Pygrr PolyArt, a program used for drawing custom Polygon models for your Pygrr project!

This is Pygrr PolyArt, a program used for drawing custom Polygon models for your Pygrr project!

Isaac 4 Dec 14, 2021
Monochromatic colorscheme for matplotlib with opinionated sensible default

Monochromatic colorscheme for matplotlib with opinionated sensible default If you need a simple monochromatic colorscheme for your matplotlib figures,

Aria Ghora Prabono 2 May 6, 2022