3

I have an interactome file composed of more than 600,000 entries. I am trying to map it using NetworkX and Matplotlib.

This is the code I am running:

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import graphviz_layout

ppi_file = r"F:\Python Project\PathLinker_2018_human-ppi-weighted-cap0_75.txt"

G = nx.DiGraph()

with open(ppi_file, "r") as file:
    next(file)
    
    for line in file:
        tail, head, edge_weight, edge_type = line.strip().split("\t")
        G.add_edge(tail, head, edge_weight=float(edge_weight), edge_type=edge_type)

pos = graphviz_layout(G)

plt.figure(figsize=(150, 150))  # Adjust the figure size as needed
nx.draw_networkx_nodes(G, pos,node_size=3000)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, arrows=False)

# Save the plot as an image file

plt.savefig("ppi_network.png", dpi=300)  # Adjust the dpi (dots per inch) as needed

plt.show()

The output is this image:

enter image description here How can I make the network clearer and not stacked on top of one another as shown in the image?

I tried increasing the dimensions of the figure multiple times, each time it's slightly better, but it everytime it's more computationally expensive. I also used the graphviz layout as many others suggested, but the output is still crammed together.

How can I fix this?

  • Did you try **sfdp**? (one of the Graphviz engines) (http://yifanhu.net/PUB/graph_draw_small.pdf). It is designed for large/very large networks. – sroush Jun 25 '23 at 17:56
  • 1
    I used Gephi before for very large graphs and it was ok. You can try GephiStreamer if you like: https://pypi.org/project/GephiStreamer/ Another option is to make interactive visualization and try to tweak the parameters until you are happy with it, I think pyVis is a good visualizer for this case: https://pyvis.readthedocs.io/en/latest/tutorial.html#visualization – Ehsan Hamzei Jun 25 '23 at 22:55
  • Btw, you can export from networkx to gephi if you like: https://stackoverflow.com/questions/46008727/how-to-store-a-networkx-graph-for-visualizing-in-gephi – Ehsan Hamzei Jun 25 '23 at 22:56
  • 3
    I wonder if there is *any* way to display 600000 nodes sensibly, other than as some sort of interactive visualization that allows you to zoom in on smaller parts. – larsks Jun 25 '23 at 23:06
  • @Muhammed You might have some success using the [spectral layout](https://networkx.org/documentation/stable/reference/generated/networkx.drawing.layout.spectral_layout.html#spectral-layout) instead – Ben Grossmann Jun 27 '23 at 20:30
  • @Muhammed To echo larsks' comment though, it's not clear what exactly you're trying to accomplish by producing an image of the graph. What aspect of the graph, exactly, is a reader supposed to ascertain from the image? For example, I don't think that the message "this is a really big, relatively densely connected network" is really made clearer with a supporting image of the entire network – Ben Grossmann Jun 27 '23 at 20:33
  • @Muhammed You might also want to consider setting the node size to zero (or perhaps to some very small number) – Ben Grossmann Jun 27 '23 at 20:36

0 Answers0