Posts

Showing posts with the label Networkx

Assign Edge Weights To A Networkx Graph Using Pandas Dataframe

Image
Answer : Let's try: import pandas as pd import numpy as np import networkx as nx import matplotlib.pyplot as plt df = pd.DataFrame({'number':['123','234','345'],'contactnumber':['234','345','123'],'callduration':[1,2,4]}) df G = nx.from_pandas_edgelist(df,'number','contactnumber', edge_attr='callduration') durations = [i['callduration'] for i in dict(G.edges).values()] labels = [i for i in dict(G.nodes).keys()] labels = {i:i for i in dict(G.nodes).keys()} fig, ax = plt.subplots(figsize=(12,5)) pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, ax = ax, labels=True) nx.draw_networkx_edges(G, pos, width=durations, ax=ax) _ = nx.draw_networkx_labels(G, pos, labels, ax=ax) Output: Do not agree with what has been said. In the calcul of different metrics that takes into account the weight of each edges like the pagerank or the betweeness centrality your weight would...

Can One Get Hierarchical Graphs From Networkx With Python 3?

Image
Answer : [scroll down a bit to see what kind of output the code produces] edit (7 Nov 2019) I've put a more refined version of this into a package I've been writing: https://epidemicsonnetworks.readthedocs.io/en/latest/_modules/EoN/auxiliary.html#hierarchy_pos. The main difference between the code here and the version there is that the code here gives all children of a given node the same horizontal space, while the code following that link also considers how many descendants a node has when deciding how much space to allocate it. edit (19 Jan 2019) I have updated the code to be more robust: It now works for directed and undirected graphs without any modification, no longer requires the user to specify the root, and it tests that the graph is a tree before it runs (without the test it would have infinite recursion - see user2479115's answer for a way to handle non-trees). edit (27 Aug 2018) If you want to create a plot with the nodes appearing as rings around th...