ericmjl / nxviz

Visualization Package for NetworkX
https://ericmjl.github.io/nxviz
MIT License
457 stars 87 forks source link

Broken example in new API documentation #661

Open MridulS opened 3 years ago

MridulS commented 3 years ago

Should the example present at https://github.com/ericmjl/nxviz/blob/master/docs/developers/architecture.md#intended-usage-example-2 be a self contained example? A user can't just copy and paste and run this example.

Something like?

from nxviz import lines, utils, layouts, nodes, edges
from nxviz.plots import despine
import matplotlib.pyplot as plt
import networkx as nx
from random import choice
import numpy as np
import pandas as pd

def group_colormap(data: pd.Series):
    cmap = {"a": "black", "b": "blue", "c": "red"}
    return data.apply(lambda x: cmap.get(x))

# Generate a random network and populate node and edge attributes.
G = nx.erdos_renyi_graph(n=20, p=0.2)
for n, d in G.nodes(data=True):
    G.nodes[n]["group"] = choice(["a", "b", "c"])
    G.nodes[n]["value"] = np.random.exponential()/10

np.random.seed(44)
for u, v, d in G.edges(data=True):
    G.edges[u, v]["edge_value"] = np.random.exponential()

ax = plt.gca()

##### Part 1: Nodes #####
# 1. Obtain node table
nt = utils.node_table(G)

# 2. Obtain positions using node table.
pos = layouts.circos(nt, group_by="group", sort_by="value")

# 3. Obtain node styles
node_color = group_colormap(nt["group"])
alpha = nodes.transparency(nt, alpha_by=None)
size = nodes.node_size(nt, "value")

# 4. Obtain patches styled correctly and add them to matplotlib axes.
patches = nodes.node_glyphs(
    nt, pos, node_color=node_color, alpha=alpha, size=size
)
for patch in patches:
    ax.add_patch(patch)

##### Part 2: Edges #####
# 1. Obtain edge table
et = utils.edge_table(G)

# 2. Obtain edge styling.
edge_color = edges.edge_colors(et, color_by=None)
lw = np.sqrt(et["edge_value"])
alpha = edges.transparency(et, alpha_by=None)

# 3. Obtain edge patches styled and add them to matplotlib axes.
patches = lines.circos(
    et, pos, edge_color=edge_color, alpha=alpha, lw=lw, aes_kw={"fc": "none"}
)
for patch in patches:
    ax.add_patch(patch)

ax.plot()
despine() # remove the spines