Home > Back-end >  How can I remove the self-loop edges from a NetworkX undirected graph plot?
How can I remove the self-loop edges from a NetworkX undirected graph plot?

Time:11-27

I thought this would be obvious but I can't seem to figure this out. I tried just making all of the self-loops a weight of 0. It looks like the order of the edges isn't preserved or something.

d = {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'weight': 100.0}, 'SRR9668968__METABAT2__P.1__bin.4': {'weight': 99.5976}}, 'SRR9668968__METABAT2__P.1__bin.4': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'weight': 99.5976}, 'SRR9668968__METABAT2__P.1__bin.4': {'weight': 100.0}}, 'SRR9668973__MAXBIN2-107__P.1__bin.001': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 99.1217}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 99.0443}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 99.1217}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 99.9955}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 100.0}}, 'SRR9668959__CONCOCT__P.1__18': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 99.0443}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 99.9955}}, 'SRR9668957__CONCOCT__P.1__5': {'SRR9668957__CONCOCT__P.1__5': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'weight': 100.0}, 'SRR9668968__CONCOCT__P.1__11': {'weight': 99.9584}}, 'SRR9668968__CONCOCT__P.1__11': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'weight': 99.9584}, 'SRR9668968__CONCOCT__P.1__11': {'weight': 100.0}}, 'SRR9668967__MAXBIN2-107__P.1__bin.001': {'SRR9668967__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'weight': 99.9547}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'SRR9668967__MAXBIN2-107__P.1__bin.001': {'weight': 99.9547}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'weight': 100.0}}, 'SRR9668973__CONCOCT__P.1__16_sub': {'SRR9668973__CONCOCT__P.1__16_sub': {'weight': 100.0}}, 'SRR9668960__CONCOCT__P.1__21': {'SRR9668960__CONCOCT__P.1__21': {'weight': 100.0}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 99.9627}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 98.9865}}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'SRR9668960__CONCOCT__P.1__21': {'weight': 99.9627}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 98.2802}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'SRR9668960__CONCOCT__P.1__21': {'weight': 98.9865}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 100.0}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 98.2802}}, 'SRR9668965__CONCOCT__P.1__23': {'SRR9668965__CONCOCT__P.1__23': {'weight': 100.0}, 'SRR9668961__METABAT2__P.1__bin.2': {'weight': 96.6062}}, 'SRR9668961__METABAT2__P.1__bin.2': {'SRR9668965__CONCOCT__P.1__23': {'weight': 96.6062}, 'SRR9668961__METABAT2__P.1__bin.2': {'weight': 100.0}}, 'SRR9668960__CONCOCT__P.1__3': {'SRR9668960__CONCOCT__P.1__3': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 99.7626}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 99.66}}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'SRR9668960__CONCOCT__P.1__3': {'weight': 99.7626}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 99.7424}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 100.0}}, 'SRR9668957__CONCOCT__P.1__38': {'SRR9668960__CONCOCT__P.1__3': {'weight': 99.66}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 99.7424}}, 'SRR9668959__METABAT2__P.1__bin.3': {'SRR9668959__METABAT2__P.1__bin.3': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__49': {'PRJNA551026-COASSEMBLY__CONCOCT__P.1__49': {'weight': 100.0}}, 'SRR9668973__METABAT2__P.1__bin.6': {'SRR9668973__METABAT2__P.1__bin.6': {'weight': 100.0}}}
graph_prok = nx.from_dict_of_dicts(d)

weights = list()
for (node_a, node_b, w) in graph_prok.edges(data="weight"):
    if node_a == node_b:
        w = 0
    weights.append(w)
weights = np.asarray(weights)*0.01

with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(8,8))
    pos = nx.nx_agraph.graphviz_layout(graph_prok, prog="neato")
    nx.draw_networkx_nodes(graph_prok,pos=pos, ax=ax)
    nx.draw_networkx_edges(graph_prok,pos=pos, ax=ax, width=weights)#, connectionstyle="arc3,rad=0")

enter image description here

CodePudding user response:

Instead of adjusting the edge weights, you can just pass an edgelist to draw_networkx_edges() and it will plot only those edges. There is a convenience function that only returns selfloops from a list of edges, so putting that together, you get:

d = {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'weight': 100.0}, 'SRR9668968__METABAT2__P.1__bin.4': {'weight': 99.5976}}, 'SRR9668968__METABAT2__P.1__bin.4': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.24': {'weight': 99.5976}, 'SRR9668968__METABAT2__P.1__bin.4': {'weight': 100.0}}, 'SRR9668973__MAXBIN2-107__P.1__bin.001': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 99.1217}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 99.0443}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 99.1217}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 99.9955}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 100.0}}, 'SRR9668959__CONCOCT__P.1__18': {'SRR9668973__MAXBIN2-107__P.1__bin.001': {'weight': 99.0443}, 'SRR9668959__CONCOCT__P.1__18': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.27': {'weight': 99.9955}}, 'SRR9668957__CONCOCT__P.1__5': {'SRR9668957__CONCOCT__P.1__5': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'weight': 100.0}, 'SRR9668968__CONCOCT__P.1__11': {'weight': 99.9584}}, 'SRR9668968__CONCOCT__P.1__11': {'PRJNA551026-COASSEMBLY__METABAT2__P.1__bin.9': {'weight': 99.9584}, 'SRR9668968__CONCOCT__P.1__11': {'weight': 100.0}}, 'SRR9668967__MAXBIN2-107__P.1__bin.001': {'SRR9668967__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'weight': 99.9547}}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'SRR9668967__MAXBIN2-107__P.1__bin.001': {'weight': 99.9547}, 'PRJNA551026-COASSEMBLY__METABAT2__P.2__bin.3': {'weight': 100.0}}, 'SRR9668973__CONCOCT__P.1__16_sub': {'SRR9668973__CONCOCT__P.1__16_sub': {'weight': 100.0}}, 'SRR9668960__CONCOCT__P.1__21': {'SRR9668960__CONCOCT__P.1__21': {'weight': 100.0}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 99.9627}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 98.9865}}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'SRR9668960__CONCOCT__P.1__21': {'weight': 99.9627}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 98.2802}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'SRR9668960__CONCOCT__P.1__21': {'weight': 98.9865}, 'PRJNA551026-COASSEMBLY__MAXBIN2-40__P.2__bin.002': {'weight': 100.0}, 'SRR9668957__MAXBIN2-107__P.1__bin.001': {'weight': 98.2802}}, 'SRR9668965__CONCOCT__P.1__23': {'SRR9668965__CONCOCT__P.1__23': {'weight': 100.0}, 'SRR9668961__METABAT2__P.1__bin.2': {'weight': 96.6062}}, 'SRR9668961__METABAT2__P.1__bin.2': {'SRR9668965__CONCOCT__P.1__23': {'weight': 96.6062}, 'SRR9668961__METABAT2__P.1__bin.2': {'weight': 100.0}}, 'SRR9668960__CONCOCT__P.1__3': {'SRR9668960__CONCOCT__P.1__3': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 99.7626}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 99.66}}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'SRR9668960__CONCOCT__P.1__3': {'weight': 99.7626}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 99.7424}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 100.0}}, 'SRR9668957__CONCOCT__P.1__38': {'SRR9668960__CONCOCT__P.1__3': {'weight': 99.66}, 'SRR9668957__CONCOCT__P.1__38': {'weight': 100.0}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__5': {'weight': 99.7424}}, 'SRR9668959__METABAT2__P.1__bin.3': {'SRR9668959__METABAT2__P.1__bin.3': {'weight': 100.0}}, 'PRJNA551026-COASSEMBLY__CONCOCT__P.1__49': {'PRJNA551026-COASSEMBLY__CONCOCT__P.1__49': {'weight': 100.0}}, 'SRR9668973__METABAT2__P.1__bin.6': {'SRR9668973__METABAT2__P.1__bin.6': {'weight': 100.0}}}
graph_prok = nx.from_dict_of_dicts(d)

edgelist = [e for e in graph_prok.edges if e not in nx.selfloop_edges(graph_prok)]

with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(8,8))
    pos = nx.nx_agraph.graphviz_layout(graph_prok, prog="neato")
    nx.draw_networkx_nodes(graph_prok,pos=pos, ax=ax)
    nx.draw_networkx_edges(graph_prok,
                           edgelist=edgelist,
                           pos=pos, ax=ax)#, connectionstyle="arc3,rad=0")

which plots:

graph_without_self_loops

  • Related