def add_graph(dims, file): count = 0 G = nx.Graph() for l in f: words = l.split(', ') G.add_edge(words[0], words[1], weight=1, relation='co-exist') count += 1 nt = Network(str(dims[0]) + 'px', str(dims[1]) + 'px') nt.from_nx(G) nt.repulsion(180, 0.5, 280) nt.show('gtsam.html') return count
def draw(net, file_name, canvas_size_100s_px=(16, 38), *args, **kwargs): # file_name set to None to leave it in matplot lib draw buffer com_grph.tracing_info_collected = False # import ipdb; ipdb.set_trace() net(*args, **kwargs) com_grph.tracing_info_collected = True labels = nx.get_node_attributes(com_grph.root_graph, 'node_tracing_name') colors = nx.get_node_attributes(com_grph.root_graph, 'color') colors = [colors[node] for node in com_grph.root_graph.nodes] pos = nx.spring_layout(com_grph.root_graph, scale=1) G = Network(f'{canvas_size_100s_px[0]*100}px', f'{canvas_size_100s_px[1]*100}px', directed=True) G.from_nx(com_grph.root_graph) G.repulsion(node_distance=0, central_gravity=0, spring_length=0, spring_strength=0, damping=0) html_filename = file_name[:-4] + '.html' G.write_html(html_filename) print(f'Interractive html file written. See {html_filename}') plt.figure(1, figsize=(canvas_size_100s_px[1], canvas_size_100s_px[0])) nx.draw(com_grph.root_graph, pos, with_labels=True, labels=labels, connectionstyle='arc3, rad = 0.1', node_color=colors, cmap=plt.cm.summer, vmin=0, vmax=1) global nodes_connected for node in nodes_connected: com_grph.remove_node(node) nodes_connected = [] if file_name is not None: plt.savefig(file_name) plt.clf() print(f'NetworkX plot saved. See {file_name}')
return "#CCCCCC" def get_borderwidth(commercial_status): if(commercial_status == "Onboarded_Solar"): return 3 else: return 0 df = pd.read_excel("VW_COMMERCIAL_BARCLAYS_INFO_202106181054.xlsx",engine='openpyxl', index_col='EMP_CODE') # df = df.iloc[0:500,:] net = Network(height='100%', width='100%', bgcolor='white', font_color='black') net.add_node("Outsider") emp_id_dict = {} for emp in df.itertuples(): net.add_node(emp.Index, label = emp.EMP_NAME, title = f"{emp.EMP_NAME} ({emp.JRSS}) - {emp.TC}, DL: {emp.IBM_POC}", color = get_color(emp.CITY), shape = get_shape(emp.BAND), borderWidth = get_borderwidth(emp.COMMERCIAL_STATUS)) emp_id_dict[emp.EMP_NOTESID] = emp.Index for emp in df.itertuples(): net.add_edge(emp.Index, emp_id_dict.get(emp.PEM_NOTESID, "Outsider")) # net.barnes_hut(gravity=-20000,central_gravity=0.05, spring_length=100, overlap=0.05) net.repulsion(node_distance=200, central_gravity=0.05, spring_length=100) # net.show_buttons(filter_=['physics']) net.show('nodes.html')
# my_obj.to_json(key+".json") # Create Network Graph/ ts_net = Network(height="100%", width="100%", bgcolor="#222222", font_color="white") # set the physics layout of the network # ts_net.repulsion(damping=0.9, spring_length=200, node_distance=350) if net_type == "atlas": ts_net.force_atlas_2based(gravity=-50, central_gravity=0.01, spring_length=100, spring_strength=0.08, damping=0.4, overlap=0) if net_type == "barnes": ts_net.barnes_hut(gravity=-80000, central_gravity=0.3, spring_length=25, spring_strength=0.001, damping=0.09, overlap=0) if net_type == "repulsion": ts_net.repulsion(damping=0.9, spring_length=200, node_distance=350) if net_type == "hrepulsion": ts_net.hrepulsion(node_distance=120, central_gravity=0.0, spring_length=100, spring_strength=0.01, damping=0.09) ts_data = pd.read_csv(key+".csv") # Call Columns in csv File by Column Names sources = ts_data['Source'] targets = ts_data['Target'] weights = ts_data['Weight'] edge_data = zip(sources, targets, weights) # add edge data
def plot(g, properties=None, selection=None, hlayout=True, scale=None, labels=None, height='800px', width='900px', **kwds): """Plot a MTG in the Jupyter Notebook""" G = Network(notebook=True, directed=True, layout=hlayout, heading="", height=height, width=width) if hlayout: G.hrepulsion() G.options.layout.hierarchical.direction = 'DU' G.options.layout.hierarchical.parentCentralization = True G.options.layout.hierarchical.levelSeparation = 150 else: G.repulsion() if scale is None: scale = g.max_scale() #Colors if cc is not None: colors = cc.glasbey_light else: colors = [ '#6e6efd', '#fb7e81', '#ad85e4', '#7be141', '#ffff00', '#ffa807', '#eb7df4', '#e6ffe3', '#d2e5ff', '#ffd1d9' ] #Data vids = g.vertices(scale=scale) edges = [(g.parent(vid), vid, 6 if g.edge_type(vid) == '<' else 1) for vid in vids if g.parent(vid) is not None ] #, 'black' if g.edge_type(vid) == '<' else None pos = g.property('position') #Level determination levels = {} root = next(g.component_roots_at_scale_iter(g.root, scale=scale)) for vid in traversal.pre_order(g, root): levels[vid] = 0 if g.parent(vid) is None else levels[g.parent(vid)] + 1 #Component roots component_roots = {} component_roots[root] = True for vid in traversal.pre_order(g, root): pid = g.parent(vid) if pid is None: component_roots[vid] = True elif g.complex(pid) != g.complex(vid): component_roots[vid] = True #Groups groups = {} for count, vid in enumerate(traversal.pre_order(g, g.complex(root))): nc = len(colors) groups[vid] = colors[count % nc] pid = g.parent(vid) if pid: if groups[vid] == groups[pid]: groups[vid] = colors[(1789 * count + 17) % nc] #Nodes adding for vid in vids: shape = 'box' if vid in component_roots else 'circle' if labels is None: label_node = g.label(vid) else: label_node = labels[vid] level = levels[vid] if selection is None: color = groups[g.complex(vid)] else: color = '#fb7e81' if vid in selection else '#97c2fc' title = dict2html(g[vid], properties=properties) #gap, mult = max(pos[1])-min(pos[1]), 20 #x = mult*pos[g.parent(vid)][0] if g.parent(vid) else pos[vid][0] # #y = mult*(gap - pos[vid][1]) #if g.parent(vid) else None #physics = False if ('edge_type' not in g[vid] or g[vid]['edge_type']=='<' or g.nb_children(vid)>0) else True G.add_node( vid, shape=shape, label=label_node, level=level, color=color, title=title, borderWidth=3, #x=x, #y=y, #physics=physics, ) #Cluster if False: for vid in traversal.pre_order(g, g.complex(root)): G.add_node(vid, hidden=True) if g.parent(vid): G.add_edge(g.parent(vid), vid, hidden=True) for cid in g.components(vid): G.add_edge(vid, cid, hidden=True) #Edges adding for edge in edges: label_edge = g.edge_type(edge[1]) G.add_edge(edge[0], edge[1], label=label_edge, width=edge[2]) return G.show('mtg.html')