def draw_circular(G, **kwargs): """Draw the graph G with a circular layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. """ draw(G, circular_layout(G), **kwargs)
def draw_circular(G, **kwargs): """Draw the graph G with a circular layout. Parameters ---------- G : graph A networkx graph **kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. """ draw(G, circular_layout(G), **kwargs)
G = NX.Graph() G.add_node("hello", size=10) G.add_node("there", size=10) G.add_node("again", size=10) G.add_node("please", weight=0.4, UTM=("13S", 382871, 3972649)) G.add_node("aa") G.add_edge("hello", "there", weight=0.6) G.add_edge("there", "again", weight=0.4) G.add_edge("there", "please", weight=0.2) G.add_edge("hello", "aa", weight=0.2) print G.nodes() print G.edges() # print G.info() print dir(lay) res01 = lay.circular_layout(G) print "res01", res01 print res02 = lay.spring_layout(G) print "res02", res02 print print "sorted! " # P.topological_sort(G) # P.topological_sort_recursive(G) nx = NX """ ANDY NOTE: requires graphviz which doesn't come easy under windows
def DrawCircular(Gtraits, G, **kwargs): fig = _Draw(Gtraits, G, circular_layout(G), **kwargs) return fig
def draw_circular(G, **kwargs): """Draw the graph G with a circular layout.""" draw(G, circular_layout(G), **kwargs)
def draw_circular(G, **kwargs): """Draw the graph G with a circular layout.""" draw(G,circular_layout(G),**kwargs)
def draw_circular(G, **kwargs): """Draw the graph G with a circular layout""" from networkx.drawing.layout import circular_layout draw(G, circular_layout(G), **kwargs)
def draw_circular(G, **kwargs): """Draw the graph G with a circular layout""" from networkx.drawing.layout import circular_layout draw(G,circular_layout(G),**kwargs)
def draw_graph(G, ax=None, circular=True): if ax is None: _, ax = plt.subplots(figsize=(9, 9)) # Compute the position of the vertices if circular: pos = layout.circular_layout(G) else: pos = layout.kamada_kawai_layout(G, weight='weight') # Get the node data to draw nodes on node_sizes = [] node_labels = {} label_colors = {} node_colors = [] linewidths = [] edgecolors = [] for v, data in G.nodes(data=True): node_sizes.append(data.get('size', 1200)) node_labels[v] = data.get('label', LOCATION_CODES[v]) node_color = data.get('color', LOCATION_COLORS[v]) node_colors.append(node_color) label_colors[v] = font_color(node_color) if data.get('halo', False): linewidths.append(8.0) edgecolors.append(data.get('halo_color', '#F4D03F')) else: linewidths.append(1.0) edgecolors.append("#666666") # Draw nodes with above properties nx.draw_networkx_nodes( G, pos=pos, ax=ax, node_size=node_sizes, node_color=node_colors, linewidths=linewidths, edgecolors=edgecolors, ) # Draw node labels by light and dark color for color in set(label_colors.values()): labels = { v: node_labels[v] for v, c in label_colors.items() if c == color } nx.draw_networkx_labels( G, pos=pos, labels=labels, font_color=color, font_size=11, font_family="serif", ) # Get edge properties to draw edges on edge_widths = [] edge_colors = [] for src, dst, data in G.edges(data=True): edge_widths.append(data.get('size', 1.0)) edge_colors.append(data.get('color', 'k')) nx.draw_networkx_edges(G, pos=pos, ax=ax, width=edge_widths, edge_color=edge_colors) # Remove grid and axes ax.grid(False) ax.axis('off') return ax
G = NX.Graph() G.add_node("hello", size=10) G.add_node("there", size=10) G.add_node("again", size=10) G.add_node("please", weight=0.4, UTM=("13S", 382871, 3972649)) G.add_node("aa") G.add_edge("hello", "there", weight=0.6) G.add_edge("there", "again", weight=0.4) G.add_edge("there", "please", weight=0.2) G.add_edge("hello", "aa", weight=0.2) print(G.nodes()) print(G.edges()) # print G.info() print(dir(lay)) res01 = lay.circular_layout(G) print("res01", res01) print() res02 = lay.spring_layout(G) print("res02", res02) print() print("sorted! ") # P.topological_sort(G) # P.topological_sort_recursive(G) nx = NX """ ANDY NOTE: requires graphviz which doesn't come easy under windows
def make_plot(self): from graphion.session.handler import get_directed # dependency cycle fix if get_directed(self.sid): G = from_pandas_adjacency(df, create_using=DiGraph) else: G = from_pandas_adjacency(df, create_using=Graph) self.nodeCount = number_of_nodes(G) """ Create NetworkX graph layout manager """ if diagramType == "FORCE": layout = spring_layout(G, k=10.42 / sqrt(self.nodeCount), seed=server.config['SEED']) elif diagramType == "HIERARCHICAL": if self.nodeCount > 1: layout = graphviz_layout(Graph([ (u, v, d) for u, v, d in G.edges(data=True) ]), prog='dot') else: layout = circular_layout( G ) # graphviz_layout does not work with one node, just display a "circular_layout" elif diagramType == "RADIAL": layout = circular_layout(G) else: pass # get node and edge information from graph nodes, nodes_coordinates = zip(*sorted(layout.items())) nodes_x, nodes_y = list(zip(*nodes_coordinates)) # calculate centrality centrality = degree_centrality(G) _, nodeCentralities = zip(*sorted(centrality.items())) if self.nodeCount > 1: # get degree information if is_directed(G): inDegreeSize = dict(G.in_degree) inDegree = inDegreeSize.copy() outDegreeSize = dict(G.out_degree) outDegree = outDegreeSize.copy() totalDegreeSize = {} for n in nodes: totalDegreeSize[n] = inDegreeSize[n] + outDegreeSize[n] totalDegree = totalDegreeSize.copy() else: inDegreeSize = dict(G.degree) inDegree = inDegreeSize.copy() outDegreeSize = inDegreeSize.copy() outDegree = inDegreeSize.copy() totalDegreeSize = inDegreeSize.copy() totalDegree = inDegreeSize.copy() # get weight information if is_directed(G): inWeightSize = dict(G.in_degree(weight='weight')) inWeight = inWeightSize.copy() outWeightSize = dict(G.out_degree(weight='weight')) outWeight = outWeightSize.copy() totalWeightSize = {} for n in nodes: totalWeightSize[n] = inWeightSize[n] + outWeightSize[n] totalWeight = totalWeightSize.copy() else: inWeightSize = dict(G.degree(weight='weight')) inWeight = inWeightSize.copy() outWeightSize = inWeightSize.copy() outWeight = inWeightSize.copy() totalWeightSize = inWeightSize.copy() totalWeight = inWeightSize.copy() # Creating a scale to ensure that the node sizes don't go bananas minNodeSize = 0.1 # minNodeSize * maxNodeSize = minimum node size maxIn = -maxsize - 1 minIn = maxsize maxOut = -maxsize - 1 minOut = maxsize maxTot = -maxsize - 1 minTot = maxsize maxInw = -maxsize - 1 minInw = maxsize maxOutw = -maxsize - 1 minOutw = maxsize maxTotw = -maxsize - 1 minTotw = maxsize for n in nodes: ind = inDegreeSize[n] outd = outDegreeSize[n] totd = totalDegreeSize[n] inw = inWeightSize[n] outw = outWeightSize[n] totw = totalWeightSize[n] if ind > maxIn: maxIn = ind elif ind < minIn: minIn = ind if outd > maxOut: maxOut = outd elif outd < minOut: minOut = outd if totd > maxTot: maxTot = totd elif totd < minTot: minTot = totd if inw > maxInw: maxInw = inw elif inw < minInw: minInw = inw if outw > maxOutw: maxOutw = outw elif outw < minOutw: minOutw = outw if totw > maxTotw: maxTotw = totw elif totw < minTotw: minTotw = totw if maxIn == minIn: sameInDegree = True else: sameInDegree = False for n in nodes: result = (inDegreeSize[n] - minIn) / maxIn if result < minNodeSize: inDegreeSize[n] = minNodeSize else: inDegreeSize[n] = result if maxOut == minOut: sameOutDegree = True else: sameOutDegree = False for n in nodes: result = (outDegreeSize[n] - minOut) / maxOut if result < minNodeSize: outDegreeSize[n] = minNodeSize else: outDegreeSize[n] = result if maxTot == minTot: sameTotalDegree = True else: sameTotalDegree = False for n in nodes: result = (totalDegreeSize[n] - minTot) / maxTot if result < minNodeSize: totalDegreeSize[n] = minNodeSize else: totalDegreeSize[n] = result if maxInw == minInw: sameInWeight = True else: sameInWeight = False for n in nodes: result = (inWeightSize[n] - minInw) / maxInw if result < minNodeSize: inWeightSize[n] = minNodeSize else: inWeightSize[n] = result if maxOutw == minOutw: sameOutWeight = True else: sameOutWeight = False for n in nodes: result = (outWeightSize[n] - minOutw) / maxOutw if result < minNodeSize: outWeightSize[n] = minNodeSize else: outWeightSize[n] = result if maxTotw == minTotw: sameTotalWeight = True else: sameTotalWeight = False for n in nodes: result = (totalWeightSize[n] - minTotw) / maxTotw if result < minNodeSize: totalWeightSize[n] = minNodeSize else: totalWeightSize[n] = result # Making a dictionary for all attributes, and ensuring none of the values go crazy. attributes = {} maxNodeSize = 30 for n in nodes: outd = outDegreeSize[n] totd = totalDegreeSize[n] inw = inWeightSize[n] outw = outWeightSize[n] totw = totalWeightSize[n] if sameInDegree: ind = 1 else: ind = inDegreeSize[n] if sameOutDegree: outd = 1 else: outd = outDegreeSize[n] if sameTotalDegree: totd = 1 else: totd = totalDegreeSize[n] if sameInWeight: inw = 1 else: inw = inWeightSize[n] if sameOutWeight: outw = 1 else: outw = outWeightSize[n] if sameTotalWeight: totw = 1 else: totw = totalWeightSize[n] attributes[n] = { 'indegreesize': ind * maxNodeSize, 'outdegreesize': outd * maxNodeSize, 'totaldegreesize': totd * maxNodeSize, 'inweightsize': inw * maxNodeSize, 'outweightsize': outw * maxNodeSize, 'totalweightsize': totw * maxNodeSize, 'indegree': inDegree[n], 'outdegree': outDegree[n], 'totaldegree': totalDegree[n], 'inweight': inWeight[n], 'outweight': outWeight[n], 'totalweight': totalWeight[n], 'count': 0 } set_node_attributes(G, attributes) plot = HVGraph.from_networkx(G, layout).opts( directed=get_directed(self.sid), arrowhead_length=0.01) # disabling displaying all node info on hovering over the node tooltips = [('Index', '@index'), ('In-Degree', '@indegree'), ('Out-Degree', '@outdegree'), ('Total Degree', '@totaldegree'), ('In Edge Weight', '@inweight'), ('Out Edge-Weight', '@outweight'), ('Total Edge-Weight', '@totalweight')] hover = HoverTool(tooltips=tooltips) else: attributes = {} for n in nodes: attributes[n] = { 'indegreesize': 1, 'outdegreesize': 1, 'totaldegreesize': 1, 'inweightsize': 1, 'outweightsize': 1, 'totalweightsize': 1, 'indegree': 0, 'outdegree': 0, 'totaldegree': 0, 'inweight': 0, 'outweight': 0, 'totalweight': 0, 'count': 0 } set_node_attributes(G, attributes) plot = HVGraph.from_networkx(G, layout).opts( directed=get_directed(self.sid), arrowhead_length=0.01) tooltips = [('Index', '@index'), ('In-Degree', '@indegree'), ('Out-Degree', '@outdegree'), ('Total Degree', '@totaldegree'), ('In Edge Weight', '@inweight'), ('Out Edge-Weight', '@outweight'), ('Total Edge-Weight', '@totalweight')] hover = HoverTool(tooltips=tooltips) # Make custom dictionary with color palettes for c in self.colorList: if c == 'cividis': self.colorMap[c] = Cividis256 elif c == 'viridis': self.colorMap[c] = Viridis256 elif c == 'inferno': self.colorMap[c] = Inferno256 else: self.colorMap[c] = palette[c] if max(nodeCentralities) > 0: if datashaded and self.nodeCount > 1: plot = bundle_graph(plot) points = plot.nodes points.opts(cmap=self.colorMap[self.color_palette], color=self.node_color, size=self.node_size, tools=['box_select', 'lasso_select', 'tap', hover], active_tools=['wheel_zoom'], toolbar='above', show_legend=False, width=self.size, height=self.size) plot.opts(node_size=0, node_color=None, node_line_width=0, node_hover_fill_color='green') return plot, points