Saturday, 9 October 2021

How to click a bubble in a bubble plot to generate a networkX network of that bubble?

I have a dataframe like this:

category source_node  target_node
cat1  node1  target1
cat1  node2  target1
cat1  node2  target2
cat1  node2  target3
cat1  node2  target4
cat2  node3  target1
cat2  node3  target1
cat2  node4  target7
cat2  node6  target7

I want to use dash (or a similar, interactive library) to first draw a bubble plot where each category is a separate bubble (ideally with a size linked to the number of items in the category but I can figure that out after), and then when you click on a bubble, a network is produced based on the source and target nodes in that bubble/category.

This is a dictionary of each category, and the source and target nodes in each category:

#A dict of source nodes (called nodes) and target nodes (called targets) that will be linked via edges in a$
node_list_dict = {'cat1':['node1','node2','target1','target2','target3','target4'],
             'cat2':['node3','node4','node6','target1','target7']}

This is a dictionary of each category, and the location of each bubble in the bubble plot:

#A dict of categories and the location of the categories in the bubble plot
node_plot_location_dict = {'cat1':[10,4],'cat2':[3,4]}

This is the code to draw the bubble plot:

#How to draw the bubble plot
fig = go.Figure(data=[go.Scatter(
    x = [node_plot_location_dict[i][0] for i in node_plot_location_dict],
    y = [node_plot_location_dict[i][1] for i in node_plot_location_dict],
    mode='markers',
    marker_size=[40, 60, 80])
])

fig.show()

And this is the code to draw a network:

#How to draw the network
import plotly.graph_objects as go
import networkx as nx
import matplotlib.pyplot as plt

dict1 = {'cat1':[{'start':'node1','end':'target1'},
                {'start':'node2','end':'target1'},
                {'start':'node2','end':'target2'},
                {'start':'node2','end':'target3'},
                {'start':'node2','end':'target4'}]}

G = nx.Graph()
for pts in dict1['cat1']:
    G.add_edge(pts['start'], pts['end'])
nx.draw(G)
plt.show()

Then, once I'm in the network, I want to click on a node in the network which will be a hyperlink to a page. Someone kindly suggested code that could be used for that purpose here.

I'm struggling to link the bubble plot to a network, so that when i 'press' a bubble in the bubble plot, a network is generated.

Could someone show me how to piece this code together so that I can use dash to make an interactive bubble plot where clicking on a bubble leads a network based on the items in that bubble?

Just to mention that I chose these libraries because they're the ones I could figure out a solution in, but I'm open to changing the library used as long as it's in python and can work in jupyter or an interface like dash.



from How to click a bubble in a bubble plot to generate a networkX network of that bubble?

No comments:

Post a Comment