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
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
Python package that generates hardware pinout diagrams as SVG images

PinOut A Python package that generates hardware pinout diagrams as SVG images. The package is designed to be quite flexible and works well for general

336 Dec 20, 2022
A Scheil-Gulliver simulation tool using pycalphad.

scheil A Scheil-Gulliver simulation tool using pycalphad. import matplotlib.pyplot as plt from pycalphad import Database, variables as v from scheil i

pycalphad 6 Dec 10, 2021
A minimalistic wrapper around PyOpenGL to save development time

glpy glpy is pyOpenGl wrapper which lets you work with pyOpenGl easily.It is not meant to be a replacement for pyOpenGl but runs on top of pyOpenGl to

Abhinav 9 Apr 02, 2022
Automate the case review on legal case documents and find the most critical cases using network analysis

Automation on Legal Court Cases Review This project is to automate the case review on legal case documents and find the most critical cases using netw

Yi Yin 7 Dec 28, 2022
Tandem Mass Spectrum Prediction with Graph Transformers

MassFormer This is the original implementation of MassFormer, a graph transformer for small molecule MS/MS prediction. Check out the preprint on arxiv

Röst Lab 13 Oct 27, 2022
matplotlib: plotting with Python

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Check out our home page for more inform

Matplotlib Developers 16.7k Jan 08, 2023
Painlessly create beautiful matplotlib plots.

Announcement Thank you to everyone who has used prettyplotlib and made it what it is today! Unfortunately, I no longer have the bandwidth to maintain

Olga Botvinnik 1.6k Jan 06, 2023
China and India Population and GDP Visualization

China and India Population and GDP Visualization Historical Population Comparison between India and China This graph shows the population data of Indi

Nicolas De Mello 10 Oct 27, 2021
Show Data: Show your dataset in web browser!

Show Data is to generate html tables for large scale image dataset, especially for the dataset in remote server. It provides some useful commond line tools and fully customizeble API reference to gen

Dechao Meng 83 Nov 26, 2022
Schema validation for Xarray objects

xarray-schema Schema validation for Xarray installation This package is in the early stages of development. Install it from source: pip install git+gi

carbonplan 22 Oct 31, 2022
Massively parallel self-organizing maps: accelerate training on multicore CPUs, GPUs, and clusters

Somoclu Somoclu is a massively parallel implementation of self-organizing maps. It exploits multicore CPUs, it is able to rely on MPI for distributing

Peter Wittek 239 Nov 10, 2022
Pglive - Pglive package adds support for thread-safe live plotting to pyqtgraph

Live pyqtgraph plot Pglive package adds support for thread-safe live plotting to

Martin Domaracký 15 Dec 10, 2022
Flexitext is a Python library that makes it easier to draw text with multiple styles in Matplotlib

Flexitext is a Python library that makes it easier to draw text with multiple styles in Matplotlib

Tomás Capretto 93 Dec 28, 2022
Profile and test to gain insights into the performance of your beautiful Python code

Profile and test to gain insights into the performance of your beautiful Python code View Demo - Report Bug - Request Feature QuickPotato in a nutshel

Joey Hendricks 138 Dec 06, 2022
A grammar of graphics for Python

plotnine Latest Release License DOI Build Status Coverage Documentation plotnine is an implementation of a grammar of graphics in Python, it is based

Hassan Kibirige 3.3k Jan 01, 2023
Epagneul is a tool to visualize and investigate windows event logs

epagneul Epagneul is a tool to visualize and investigate windows event logs. Dep

jurelou 190 Dec 13, 2022
An intuitive library to add plotting functionality to scikit-learn objects.

Welcome to Scikit-plot Single line functions for detailed visualizations The quickest and easiest way to go from analysis... ...to this. Scikit-plot i

Reiichiro Nakano 2.3k Dec 31, 2022
Lumen provides a framework for visual analytics, which allows users to build data-driven dashboards from a simple yaml specification

Lumen project provides a framework for visual analytics, which allows users to build data-driven dashboards from a simple yaml specification

HoloViz 120 Jan 04, 2023
TensorDebugger (TDB) is a visual debugger for deep learning. It extends TensorFlow with breakpoints + real-time visualization of the data flowing through the computational graph

TensorDebugger (TDB) is a visual debugger for deep learning. It extends TensorFlow (Google's Deep Learning framework) with breakpoints + real-time visualization of the data flowing through the comput

Eric Jang 1.4k Dec 15, 2022
Getting started with Python, Dash and Plot.ly for the Data Dashboards team

data_dashboards Getting started with Python, Dash and Plot.ly for the Data Dashboards team Getting started MacOS users: # Install the pyenv version ma

Department for Levelling Up, Housing and Communities 1 Nov 08, 2021