def plot(part): n = makenode(part) proc = n.graph() G = nx.Graph() G.add_edges_from(proc) # TODO: can this be done without reading and writing a file? nx.write_edgelist(G, path="grid.edgelist", delimiter=":") H = nx.read_edgelist(path="grid.edgelist", delimiter=":") os.unlink("grid.edgelist") layout = l.kamada_kawai_layout(G, pos=l.shell_layout(G)) nx.draw( H, pos=layout, edge_color="#777777", node_color="#ffffff", with_labels=True, font_family="Arial", font_size=9, label=part, ) #plt.show() if not os.path.isdir('output'): os.makedirs('output') path = os.path.join('output', part + '.svg') plt.savefig(path, format="svg") plt.close()
def calculate2d(self, force=False, scale=1): """ recalculate 2d coordinates. currently rings can be calculated badly. :param scale: rescale calculated positions. :param force: ignore existing coordinates of atoms """ dist = {} # length forces for n, m_bond in self._adj.items(): dist[n] = {} for m in m_bond: dist[n][m] = .825 # angle forces for n, m_bond in self._adj.items(): if len(m_bond) == 2: # single-single or single-double bonds has angle = 120, other 180 (m1, b1), (m2, b2) = m_bond.items() dist[m1][m2] = dist[m2][m1] = 1.43 if b1.order + b2.order in (2, 3) else 1.7 # +.05 elif len(m_bond) == 3: m1, m2, m3 = m_bond dist[m1][m2] = dist[m1][m3] = dist[m2][m3] = dist[m3][m2] = dist[m2][m1] = dist[m3][m1] = 1.43 elif len(m_bond) == 4: # 1 # # 2 X 4 # # 3 m1, m2, m3, m4 = m_bond dist[m1][m2] = dist[m1][m4] = dist[m2][m1] = dist[m2][m3] = 1.17 dist[m3][m2] = dist[m3][m4] = dist[m4][m1] = dist[m4][m3] = 1.17 dist[m1][m3] = dist[m3][m1] = dist[m2][m4] = dist[m4][m2] = 1.7 # +.05 # cycle forces for r in self.sssr: if len(r) == 6: # 6 # # 1 5 # # 2 4 # # 3 m1, m2, m3, m4, m5, m6 = r dist[m1][m4] = dist[m4][m1] = dist[m2][m5] = dist[m5][m2] = dist[m3][m6] = dist[m6][m3] = 1.7 # +.05 if force: pos = None else: pos = {n: (atom.x or uniform(0, .01), atom.y or uniform(0, .01)) for n, atom in self.atoms()} for n, xy in kamada_kawai_layout(self, dist=dict(dist), pos=pos, scale=scale).items(): atom = self._node[n] atom.x, atom.y = xy self.flush_cache()
def draw_kamada_kawai(G, **kwargs): """Draw the graph G with a Kamada-Kawai force-directed 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, kamada_kawai_layout(G), **kwargs)
def draw_kamada_kawai(G, **kwargs): """Draw the graph G with a Kamada-Kawai force-directed 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, kamada_kawai_layout(G), **kwargs)
# %% import networkx as nx # from networkx.algorithms import centrality from networkx.readwrite import gexf import pandas as pd import matplotlib.pyplot as plt from networkx.drawing import layout ## https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.nx_agraph.graphviz_layout.html # %% print('start') G = gexf.read_gexf('3mj/subgraph--1-start.gexf') print('generate layout') sp = layout.kamada_kawai_layout(G) degrees = pd.DataFrame(dict(G.degree).items(), columns=['tag', 'degree']) print(nx.__version__) print('before') print(nx.info(G)) nodes_zero_degree = (degrees.query('degree == 0')['tag'].to_list()) G.remove_nodes_from(nodes_zero_degree) print('after') print(nx.info(G)) # %% df_decompose = pd.read_csv('3mj/decomposed_3mj.csv')[[ 'iteration', 'tag', 'filename' ]]
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