Skip to content

Binder

Annotating Node Labels on Matrix Plots.

Author: Alireza Hosseini

from random import randint

import matplotlib.pyplot as plt
import networkx as nx

import nxviz as nv
from nxviz import annotate

G = nx.barbell_graph(m1=10, m2=3)
for n, d in G.nodes(data=True):
    G.nodes[n]["group"] = randint(0, 3)
G = nx.relabel_nodes(G, {i: "long name #" + str(i) for i in range(len(G))})

fig, ax = plt.subplots(figsize=(10, 10))
nv.matrix(G, group_by="group", node_color_by="group")
annotate.matrix_labels(G, group_by="group", layout="standard")
# the standard labels take up more space, so we will have to increase the
# padding a bit. 5% on all sides works well here.
plt.tight_layout(rect=(0.05, 0.05, 0.95, 0.95))
plt.show()
/home/runner/work/nxviz/nxviz/nxviz/__init__.py:33: UserWarning: 
nxviz has a new API! Version 0.7.4 onwards, the old class-based API is being
deprecated in favour of a new API focused on advancing a grammar of network
graphics. If your plotting code depends on the old API, please consider
pinning nxviz at version 0.7.4, as the new API will break your old code.

To check out the new API, please head over to the docs at
https://ericmjl.github.io/nxviz/ to learn more. We hope you enjoy using it!

(This deprecation message will go away in version 1.0.)

  warnings.warn(

No description has been provided for this image