def plot_network(graph, title): plt.axis('off') pos = graphviz_layout(graph) nx.draw_networkx(graph, pos=pos, with_labels=False, **dzcnapy.small_attrs) depth = max(nx.get_node_attributes(graph, 'generation').values()) + 1 colors = get_colors(depth) color = [colors[graph.node[n]["generation"]] for n in graph] dzcnapy.small_attrs["node_color"] = color patchList = [] for index in range(depth): data_key = mpatches.Patch(color=colors[index], label='Generation {}'.format(index)) patchList.append(data_key) plt.legend(handles=patchList) dzcnapy.set_extent(pos, plt) dzcnapy.plot(title, [15, 15], save=True)
G4 = nx.star_graph(20) G5 = nx.complete_graph(20) G2 = nx.balanced_tree(2, 5) G3 = nx.grid_2d_graph(5, 4) names = ("Linear (path)", "Ring (Cycle)", "Balanced Tree", "Mesh (Grid)", "Star", "Complete") graphs = G0, G1, G2, G3, G4, G5 layouts = (graphviz_layout, ) * len(graphs) for i, (g, name, layout) in enumerate(zip(graphs, names, layouts)): ax = plt.subplot(3, 2, i + 1) pos = layout(g) nx.draw_networkx_edges(g, pos, alpha=0.5, ax=ax, **dzcnapy.small_attrs) nx.draw_networkx_nodes(g, pos, ax=ax, **dzcnapy.small_attrs) dzcnapy.set_extent(pos, ax, name) dzcnapy.plot("synthetic3") #Generate and draw random networks G0 = nx.erdors_renyi_graph(50, 0.05) G1 = nx.connected_watts_strogatz_graph(50, 4, 0.05) G2 = nx.barabasi_albert_graph(50, 4) G3 = nx.powerlaw_cluster_graph(50, 4, 0.05) names = ("Erdos-Renvi (p=0.05)", "Watts-Strogatz (k=4, p=0.5)", "Barabasi-Albert (k=4)", "Holme-Kim (k=4, p=0.5)") graphs = G0, G1, G2, G3 layouts = (nx.circular_layout, nx.circular_layout, graphviz_layout, graphviz_layout)
import networkx as nx, community, csv import matplotlib.style as style, matplotlib.pyplot as plt from networkx.drawing.nx_agraph import graphviz_layout import dzcnapy_plotlib as dzcnapy F = nx.DiGraph() F.add_node("C") F.add_edges_from([("B", "b0"), ("b0", "b1"), ("b1", "B")]) F.add_edges_from([("A", "a0"), ("a0", "a1"), ("a1", "a2"), ("a1", "a3"), ("a3", "A")]) pos = graphviz_layout(F) nx.draw_networkx(F, pos, **dzcnapy.attrs) dzcnapy.set_extent(pos, plt) dzcnapy.plot("abcNwtwork") G = nx.Graph( (("Alpha", "Bravo"), ("Bravo", "Charlie"), ("Charlie", "Delta"), ("Charlie", "Echo"), ("Charlie", "Foxtrot"), ("Delta", "Echo"), ("Delta", "Foxtrot"), ("Echo", "Foxtrot"), ("Echo", "Golf"), ("Echo", "Hotel"), ("Foxtrot", "Golf"), ("Foxtrot", "Hotel"), ("Delta", "Hotel"), ("Golf", "Hotel"), ("Delta", "India"), ("Charlie", "India"), ("India", "Juliet"), ("Golf", "Kilo"), ("Alpha", "Kilo"), ("Bravo", "Lima"))) pos = graphviz_layout(G) core = nx.k_core(G) crust = nx.k_crust(G) corona3 = nx.k_corona(G, k=3).nodes() nx.draw_networkx(G, pos, nodelist=core, **dzcnapy.attrs) nx.draw_networkx_edges(G, pos, core.edges(), **dzcnapy.tick_attrs) nx.draw_networkx_edges(G, pos, crust.edges(), **dzcnapy.tick_attrs) nx.draw_networkx_nodes(G, pos, crust, node_shape='v', **dzcnapy.attrs)
def slice_network(G, T, copy=True): """ Remove all edges with weight<T from G or its copy. """ F = G.copy() if copy else G F.remove_edges_from( (n1, n2) for n1, n2, w in F.edges(data="weight") if w < T) return F # Draw different degrees of slicing for a random graph ergraph = nx.erdos_renyi_graph(100, 0.1) nx.set_edge_attributes(ergraph, "weight", {(n1, n2): random.random() for n1, n2 in ergraph.edges()}) pos = graphviz_layout(ergraph) for i, threshold in enumerate([0, 0.7, 0.8, 0.9, 0.95, 1.0]): ax = plt.subplot(3, 2, i + 1) slice_network(ergraph, threshold, copy=False) nx.draw_networkx_edges(ergraph, pos, alpha=0.65, ax=ax, **dzcnapy.small_attrs) nx.draw_networkx_nodes(ergraph, pos, ax=ax, **dzcnapy.small_attrs) dzcnapy.set_extent(pos, ax, "Threshold={}".format(threshold)) dzcnapy.plot("slicing")