简体   繁体   中英

How to show number of connections as edge label and weight based on number of connections

How to show the number of connections as edge label and edge weight based on number of connections.

My code so far:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.DiGraph()

G.add_edge('a','b');
G.add_edge('c','d');
G.add_edge('c','a');
G.add_edge('a','b');
G.add_edge('b','a');

nx.draw(G,with_labels=True)

plt.show()

My objective is to create a graph based on a large amount of data (Python script)

newbie to networkx and matplotlib

The basic problem that you run into, is that networkx is not really suited for plotting weighted, directed graphs. It can be done, but it ain't pretty. In particular, there is no real support for plotting bi-directional connections, as bi-directional connections are plotted on top of each other. Below I try to get around that by plotting the edge weight near the head of the connection, but that doesn't work really well.

在此处输入图片说明

import matplotlib.pyplot as plt
import networkx as nx

G=nx.MultiDiGraph()

G.add_edge('a','b');
G.add_edge('c','d');
G.add_edge('c','a');
G.add_edge('a','b');
G.add_edge('b','a');

edge_to_count = dict()
for edge in G.edges():
    try:
        edge_to_count[edge] += 1
    except KeyError:
        edge_to_count[edge] = 1

pos = nx.spring_layout(G)
nx.draw(G, pos)
nx.draw_networkx_edge_labels(G, pos, edge_to_count, label_pos=0.25)

plt.show()

If you want to represent the multi-edges as an edge weight, then the code changes to:

import matplotlib.pyplot as plt
import networkx as nx

# get edge weights based on edge count
edges = [('a','b'),
         ('c','d'),
         ('c','a'),
         ('a','b'),
         ('b','a')]

edge_to_count = dict()
for edge in edges:
    try:
        edge_to_count[edge] += 1.
    except KeyError:
        edge_to_count[edge] = 1.

# convert to networkx edge list format
edges = [(k[0], k[1], dict(weight=v)) for k, v in edge_to_count.items()]

# create graph
G = nx.DiGraph()
G.add_edges_from(edges)

# precompute layout to be used by both, draw() and draw_networkx_edge_labels
pos = nx.spring_layout(G)

# draw graph first, then labels on top
nx.draw(G, pos)
nx.draw_networkx_edge_labels(G, pos, edge_to_count, label_pos=0.25)

plt.show()

Addendum

At some point, I battled with this exact problem for a while so I wrote my own little network plotting library called netgraph (available via pip and on github ). The API is very similar to networkx, with the exception that I got rid of some naming inconsistencies that I suspect were kept in networkx for backwards compatibility. The default output looks like this:

在此处输入图片说明

As you can see, it supports plotting of bi-directional / parallel edges. Edges are colored according to their weight by default. As it accepts networkx graph objects as input, it's fairly easy to integrate with networkx (it supports a variety of input formats as well).

import matplotlib.pyplot as plt
import networkx
import netgraph

# get edge weights based on edge count
edges = [('a','b'),
         ('c','d'),
         ('c','a'),
         ('a','b'),
         ('b','a')]

edge_to_count = dict()
for edge in edges:
    try:
        edge_to_count[edge] += 1.
    except KeyError:
        edge_to_count[edge] = 1.

# convert to networkx edge list format
edges = [(k[0], k[1], dict(weight=v)) for k, v in edge_to_count.items()]

# create graph
G = nx.DiGraph()
G.add_edges_from(edges)

# draw
netgraph.draw(G, edge_labels=edge_to_count, edge_label_font_size=8)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM