1

I am generating a graph to visualize the association's rules and using pyviz to get an interactive visualization. Currently, I saved data in one graph but I would like to save different data into separate graphs and represent it all in one network graph, then set a drop-down menu to filter the graph and only show that part of the whole network graph.

I would like to have, graph_1,graph_2, and graph_3 saved in one general graph and do post filtering where I can choose which to focus.

I am using the below code to generate and visualize one graph:

import networkx as nx  
from pyvis.network import Network

def draw_graph(rules, saveName):
    
    rules_to_show = len(rules)
    
    G1 = nx.DiGraph()  
    color_map=[]
    N = 50
    colors = np.random.rand(N)    
    strs=[]
    mydic={}

    median = rules['confidence'].median()

    
    for i in range (rules_to_show):
        conf = rules.iloc[i]['confidence']
        strs.append(str('R')+str(i))
        
        G1.add_nodes_from(["R"+str(i)])
        
        for a in rules.iloc[i]['antecedents']:
                
            G1.add_nodes_from([a])
        
            if conf > median:
                G1.add_edge(a, "R"+str(i), color='lightgrey' , value = conf*0.01)
            else:
                G1.add_edge(a, "R"+str(i), color='orange' , value = conf*0.01)
       
        for c in rules.iloc[i]['consequents']:
             
            G1.add_nodes_from([a])
            
            if conf > median:
                G1.add_edge("R"+str(i), c, color='lightgrey', value = conf*0.01)
            else:
                G1.add_edge("R"+str(i), c, color='orange', value = conf*0.01)
 
    for node in G1:
#         print(node)
        found_a_string = False
        for item in strs:
#             print(item)
            if node==item:
                found_a_string = True
        if found_a_string:
            color_map.append("lightblue")
        else:
            color_map.append("lightgreen")  
            

    for index,row in dataset.iterrows():
        mydic.update({row["items"]:(row['support'])})
    
    x={}
    for node in G1:
        if node in mydic:
            
            x.update({node:mydic.get(node)})
        else:
            x.setdefault(node, 0.0001)
            
        
    nodes, values = zip(*x.items())
              

    nt= Network(notebook=True,
    cdn_resources="remote",
    bgcolor="#222222",
    font_color="white",
    height="750px",
    width="100%",
    select_menu=True,
    filter_menu=True)
    

    nt.add_nodes(list(nodes), value=[int(v*10000) for v in values], color=color_map)

    nt.from_nx(G1)
    nt.repulsion(node_distance=100, spring_length=500)
    nt.show_buttons(filter_=['physics'])
    nt.show(str(saveName)+'.html')

    
draw_graph (update_df, 'test')  
rooya sh
  • 37
  • 7

0 Answers0