simjas - 9 months ago 64
Python Question

to show the weights of edges in a projected network

I have projected a bipartite graph and made a new weighted graph. I would like to draw the graph, and show the edge weights.

Here is my attempt.

import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import bipartite
g=nx.Graph()
l=bipartite.weighted_projected_graph(g,['s1','s2','s3','s4','s5'])
nx.draw(l, with_labels=True)
plt.show()


The node labels appear, but not the edge weights. How can I show the edge weights?

I've modified your code to include nx.draw_networkx_edge_labels

import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import bipartite
g=nx.Graph()

This will probably show more than you want. I think it's set up for cases where edges may have somewhat arbitrary attributes. I think what you want is to do everything up to defining pos and then:
edge_weights = {(u,v,):d['weight'] for u,v,d in l.edges(data=True)}