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
在原神中使用围栏绘图

yuanshen_draw 在原神中使用围栏绘图 文件说明 toLines.py 将一张图片转换为对应的线条集合,视频可以按帧转换。 draw.py 在原神家园里绘制一张线条图。 draw_video.py 在原神家园里绘制视频(自动按帧摆放,截图(win)并回收) cat_to_video.py

14 Oct 08, 2022
AB-test-analyzer - Python class to perform AB test analysis

AB-test-analyzer Python class to perform AB test analysis Overview This repo con

13 Jul 16, 2022
Visualize large time-series data in plotly

plotly_resampler enables visualizing large sequential data by adding resampling functionality to Plotly figures. In this Plotly-Resampler demo over 11

PreDiCT.IDLab 604 Dec 28, 2022
Small binja plugin to import header file to types

binja-import-header (v1.0.0) Author: matteyeux Import header file to Binary Ninja types view Description: Binary Ninja plugin to import types from C h

matteyeux 15 Dec 10, 2022
YOPO is an interactive dashboard which generates various standard plots.

YOPO is an interactive dashboard which generates various standard plots.you can create various graphs and charts with a click of a button. This tool uses Dash and Flask in backend.

ADARSH C 38 Dec 20, 2022
Leyna's Visualizing Data With Python

Leyna's Visualizing Data Below is information on the number of bilingual students in three school districts in Massachusetts. You will also find infor

11 Oct 28, 2021
OpenStats is a library built on top of streamlit that extracts data from the Github API and shows the main KPIs

Open Stats Discover and share the KPIs of your OpenSource project. OpenStats is a library built on top of streamlit that extracts data from the Github

Pere Miquel Brull 4 Apr 03, 2022
Matplotlib tutorial for beginner

matplotlib is probably the single most used Python package for 2D-graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats. We are goi

Nicolas P. Rougier 2.6k Dec 28, 2022
This Crash Course will cover all you need to know to start using Plotly in your projects.

Plotly Crash Course This course was designed to help you get started using Plotly. If you ever felt like your data visualization skills could use an u

Fábio Neves 2 Aug 21, 2022
Lightspin AWS IAM Vulnerability Scanner

Red-Shadow Lightspin AWS IAM Vulnerability Scanner Description Scan your AWS IAM Configuration for shadow admins in AWS IAM based on misconfigured den

Lightspin 90 Dec 14, 2022
Fast data visualization and GUI tools for scientific / engineering applications

PyQtGraph A pure-Python graphics library for PyQt5/PyQt6/PySide2/PySide6 Copyright 2020 Luke Campagnola, University of North Carolina at Chapel Hill h

pyqtgraph 3.1k Jan 08, 2023
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
Automatically visualize your pandas dataframe via a single print! 📊 💡

A Python API for Intelligent Visual Discovery Lux is a Python library that facilitate fast and easy data exploration by automating the visualization a

Lux 4.3k Dec 28, 2022
A tool for creating SVG timelines from simple JSON input.

A tool for creating SVG timelines from simple JSON input.

Jason Reisman 432 Dec 30, 2022
Material for dataviz course at university of Bordeaux

Material for dataviz course at university of Bordeaux

Nicolas P. Rougier 50 Jul 17, 2022
📊📈 Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

📊📈 Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

wq framework 1.2k Jan 01, 2023
Manim is an animation engine for explanatory math videos.

A community-maintained Python framework for creating mathematical animations.

12.4k Dec 30, 2022
A small timeseries transformation API built on Flask and Pandas

#Mcflyin ###A timeseries transformation API built on Pandas and Flask This is a small demo of an API to do timeseries transformations built on Flask a

Rob Story 84 Mar 25, 2022
`charts.css.py` brings `charts.css` to Python. Online documentation and samples is available at the link below.

charts.css.py charts.css.py provides a python API to convert your 2-dimension data lists into html snippet, which will be rendered into charts by CSS,

Ray Luo 3 Sep 23, 2021
visualize_ML is a python package made to visualize some of the steps involved while dealing with a Machine Learning problem

visualize_ML visualize_ML is a python package made to visualize some of the steps involved while dealing with a Machine Learning problem. It is build

Ayush Singh 164 Dec 12, 2022