import networkx as nx import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt import numpy as np exec(open("dod.py").read()) UG = nx.Graph(dod) DG = nx.DiGraph(dod) nnodes = DG.number_of_nodes() # nx.draw(G, with_labels=True);plt.show() metrics = [ (UG, nx.eigenvector_centrality), (UG, nx.pagerank), (DG, nx.pagerank), ] results = [sorted(metric(G).items(), key=lambda k: k[1], reverse=True) for G, metric in metrics] x = [i+1 for i in range(nnodes)] y = lambda rs: [v for _, v in rs] plt.loglog(x, y(results[0]), label="eigen Undirected", color='red', linestyle='-.') plt.loglog(x, y(results[1]), label="pagerank Undirected", color='blue', linestyle='--') plt.loglog(x, y(results[2]), label="pagerank Directed", color='blue', linestyle='-') plt.legend() plt.xlabel("packages") plt.ylabel("metric value") plt.grid() plt.xlim([1, nnodes+2]) plt.ylim([1e-5, 1]) plt.show() def common(results, n): packages = results[0][:2*n].copy() for result in results[1:]: for name, value in packages: if not name in [name for name, _ in result]: packages.remove((name, value)) return [name for name, _ in packages[:n]]