title: Python/NetworkX: calculate edge weights on the fly
body: <p>I have an <strong>unweighted</strong> graph created with <code>networkx</code> for which I would like to calculate the weight of edges between nodes based on the count/frequency of an edge occurrence. An edge in my graph can occur more than once but the frequency of an edge appearance is not known in advance. The purpose is to visualize the edges based on the weight (e.g. count/frequency) of moves between connected nodes. Essentially, I'd like to create a network traffic map of movement between connected nodes, and visualize based on color or edge width. E.g., edge from node 0 to 1 has 10 movements between them, and node 1 to 2 has 5, so edge 0-1 would be visualized using a different edge color/size.</p>
  
  <p>How can I calculate the weight of edges between two nodes, on the fly (after adding them to the graph with <code>g.add_edges_from()</code>), and then reapply to my graph for visualization? Below is a sample of my graph, data, and code I've used to create the graph initially and a solution I attempted that failed. </p>
  
  <p><strong>Graph</strong></p>
  
  <p><a href="https://i.stack.imgur.com/PlV1e.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PlV1e.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Sample Data</strong></p>
  
  <p>Cluster centroids(nodes)</p>
  
  <pre><code>cluster_label,latitude,longitude
  0,39.18193382,-77.51885109
  1,39.18,-77.27
  2,39.17917928,-76.6688633
  3,39.1782,-77.2617
  4,39.1765,-77.1927
  5,39.1762375,-76.8675441
  6,39.17468,-76.8204499
  7,39.17457332,-77.2807235
  8,39.17406072,-77.274685
  9,39.1731621,-77.2716502
  10,39.17,-77.27
  </code></pre>
  
  <p>Trajectories(edges)</p>
  
  <pre><code>user_id,trajectory
  11011.0,"[[340, 269], [269, 340]]"
  80973.0,"[[398, 279]]"
  608473.0,"[[69, 28]]"
  2139671.0,"[[382, 27], [27, 285]]"
  3945641.0,"[[120, 422], [422, 217], [217, 340], [340, 340]]"
  5820642.0,"[[458, 442]]"
  6060732.0,"[[291, 431]]"
  6912362.0,"[[68, 27]]"
  7362602.0,"[[112, 269]]"
  8488782.0,"[[133, 340], [340, 340]]"
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import csv
  import networkx as nx
  import pandas as pd
  import community
  import matplotlib.pyplot as plt
  import time
  import mplleaflet
  
  g = nx.MultiGraph()
  
  df = pd.read_csv('cluster_centroids.csv', delimiter=',')
  df['pos'] = list(zip(df.longitude,df.latitude))
  dict_pos = dict(zip(df.cluster_label,df.pos))
  #print dict_pos
  
  for row in csv.reader(open('edges.csv', 'r')):
      if '[' in row[1]:       #
          g.add_edges_from(eval(row[1]))
  
  # Plotting with mplleaflet
  fig, ax = plt.subplots()
  nx.draw_networkx_nodes(g,pos=dict_pos,node_size=50,node_color='b')
  nx.draw_networkx_edges(g,pos=dict_pos,linewidths=0.01,edge_color='k', alpha=.05)
  nx.draw_networkx_labels(g,dict_pos)
  mplleaflet.show(fig=ax.figure)
  </code></pre>
  
  <p>I have tried using <code>g.add_weighted_edges_from()</code> and adding <code>weight=1</code> as an attribute, but have not had any luck. I also tried using this which also did not work:</p>
  
  <pre><code>for u,v,d in g.edges():
      d['weight'] = 1
  g.edges(data=True)
  edges = g.edges()
  weights = [g[u][v]['weight'] for u,v in edges]
  </code></pre>