Bipartite graph in python
I have a dictionary data structure. I will like to draw a bipartite graph to visualise the data. Am new to python. I have checked online but dint find any useful help in this regard. I know I will be using the network module in python for this. My data is in the format [Targets:drugs]
Where targets are keys and drugs are values.
• 6,357 views
•
link
1 answer
I am not a python programmer so I'll do this as an exercise:
import networkx as nx
import matplotlib.pyplot as plt # For drawing
# Dictionary of target:drug
dictionary = { 'ALK': 'Crizotinib', 'EGFR': 'Lapatinib', 'VEGF':'Regorafenib', 'HER2': 'Lapatinib', 'BCR-ABL': 'Ponatinib', 'RET':'Regorafenib' }
G=nx.Graph() # Create a graph
for target in dictionary.keys():
G.add_edge(target, dictionary[target]) # Add an edge for each dictionary entry
# Nodes are automatically created
pos = nx.spring_layout(G) # Layout algorithm
nx.draw(G, with_labels = True) # Draw the graph
plt.savefig("graph.png") # Save to a PNG file
Note that a dictionary doesn't allow duplicate keys so it can't represent targets with more than one drug unless the values are lists of drugs.
• 0 views
•
link
Log in to answer this question.