def setUp(self): G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted") G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted") G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted") self.G = nx.union(G1, G2) self.G = nx.union(self.G, G3) self.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)]) self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1) self.gc = [] G = nx.DiGraph() G.add_edges_from([(1, 2), (2, 3), (2, 8), (3, 4), (3, 7), (4, 5), (5, 3), (5, 6), (7, 4), (7, 6), (8, 1), (8, 7)]) C = [[3, 4, 5, 7], [1, 2, 8], [6]] self.gc.append((G, C)) G = nx.DiGraph() G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)]) C = [[2, 3, 4],[1]] self.gc.append((G, C)) G = nx.DiGraph() G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)]) C = [[1, 2, 3]] self.gc.append((G,C)) # Eppstein's tests G = nx.DiGraph({0:[1], 1:[2, 3], 2:[4, 5], 3:[4, 5], 4:[6], 5:[], 6:[]}) C = [[0], [1], [2],[ 3], [4], [5], [6]] self.gc.append((G,C)) G = nx.DiGraph({0:[1], 1:[2, 3, 4], 2:[0, 3], 3:[4], 4:[3]}) C = [[0, 1, 2], [3, 4]] self.gc.append((G, C))
def __init__(self): wx.Frame.__init__(self, None, -1) self.panel = wx.Panel(self) self.fig = plt.figure() self.canvas = FigCanvas(self.panel, -1, self.fig) G = nx.house_graph() pos = {0: (0, 0), 1: (1, 0), 2: (0, 1), 3: (1, 1), 4: (0.5, 2.0)} nx.draw_networkx_nodes(G, pos, node_size=2000, nodelist=[4]) nx.draw_networkx_nodes(G, pos, node_size=3000, nodelist=[0, 1, 2, 3], node_color='b') nx.draw_networkx_edges(G, pos, alpha=0.5, width=6) plt.axis('off') self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) self.toolbar = NavigationToolbar(self.canvas) self.vbox.Add(self.toolbar, 0, wx.EXPAND) self.panel.SetSizer(self.vbox) self.vbox.Fit(self) # plt.savefig("house_with_colors.png") # save as png
def HouseGraph(): """ Returns a house graph with 5 nodes. A house graph is named for its shape. It is a triangle (roof) over a square (walls). This constructor depends on NetworkX numeric labeling. PLOTTING: Upon construction, the position dictionary is filled to override the spring-layout algorithm. By convention, the house graph is drawn with the first node in the lower-left corner of the house, the second in the lower-right corner of the house. The third node is in the upper-left corner connecting the roof to the wall, and the fourth is in the upper-right corner connecting the roof to the wall. The fifth node is the top of the roof, connected only to the third and fourth. EXAMPLES: Construct and show a house graph :: sage: g = graphs.HouseGraph() sage: g.show() # long time """ pos_dict = {0:(-1,0),1:(1,0),2:(-1,1),3:(1,1),4:(0,2)} import networkx G = networkx.house_graph() return graph.Graph(G, pos=pos_dict, name="House Graph")
def test_line_graph_batch(): """ Tests that `line_graph` works with multiple graphs in a batch. """ # Arrange. # Create test graphs. graph1 = nx.house_graph() incidence1 = nx.incidence_matrix(graph1).toarray() # We will have to pad the first incidence matrix with two columns, # because the second graph has two extra edges. incidence1 = np.pad(incidence1, ((0, 0), (0, 2))) graph2 = nx.house_x_graph() incidence2 = nx.incidence_matrix(graph2).toarray() # Act. # Get the line graph for both graphs individually, and both together # as a batch. graph1_line = convolution.line_graph(incidence1) graph2_line = convolution.line_graph(incidence2) batch = np.stack([incidence1, incidence2], axis=0) line_graph_batch = convolution.line_graph(batch) # Assert. # Both ways of doing it should produce the same results. np.testing.assert_array_equal(graph1_line, line_graph_batch[0]) np.testing.assert_array_equal(graph2_line, line_graph_batch[1])
def HouseGraph(): """ Returns a house graph with 5 nodes. A house graph is named for its shape. It is a triangle (roof) over a square (walls). This constructor depends on NetworkX numeric labeling. PLOTTING: Upon construction, the position dictionary is filled to override the spring-layout algorithm. By convention, the house graph is drawn with the first node in the lower-left corner of the house, the second in the lower-right corner of the house. The third node is in the upper-left corner connecting the roof to the wall, and the fourth is in the upper-right corner connecting the roof to the wall. The fifth node is the top of the roof, connected only to the third and fourth. EXAMPLES: Construct and show a house graph :: sage: g = graphs.HouseGraph() sage: g.show() # long time """ pos_dict = {0: (-1, 0), 1: (1, 0), 2: (-1, 1), 3: (1, 1), 4: (0, 2)} import networkx G = networkx.house_graph() return graph.Graph(G, pos=pos_dict, name="House Graph")
def test_incidence_matrix_batch(): """ Tests that `incidence_matrix` works with multiple graphs in a batch. """ # Arrange. # Create test graphs. graph1 = nx.to_numpy_array(nx.house_graph()) graph2 = nx.to_numpy_array(nx.house_x_graph()) # Act. # Get the incidence matrix for both graphs individually, and both together # as a batch. graph1_incidence = convolution.incidence_matrix(graph1) # The first graph will be padded with two extra rows columns so that it # can be batched with the other one, which has two more edges. graph1_incidence = np.pad(graph1_incidence, ((0, 0), (0, 2))) graph2_incidence = convolution.incidence_matrix(graph2) batch = np.stack([graph1, graph2], axis=0) batch_incidence = convolution.incidence_matrix(batch) # Assert. # Both ways of doing it should produce the same results. np.testing.assert_array_equal(graph1_incidence, batch_incidence[0]) np.testing.assert_array_equal(graph2_incidence, batch_incidence[1])
def setUp(self): self.graph = nx.house_graph().to_directed() self.path = [3, 4] self.road_network = RoadNetwork(self.graph, None) router = BaseRouter(self.road_network, beta=np.log(10)) router.paths_for_od = lambda r, s: (self.path for x in xrange(1)) self.subject = router.route_for_od(3, 4)
def setUp(self): G1=cnlti(nx.grid_2d_graph(2,2),first_label=0,ordering="sorted") G2=cnlti(nx.lollipop_graph(3,3),first_label=4,ordering="sorted") G3=cnlti(nx.house_graph(),first_label=10,ordering="sorted") self.G=nx.union(G1,G2) self.G=nx.union(self.G,G3) self.DG=nx.DiGraph([(1,2),(1,3),(2,3)]) self.grid=cnlti(nx.grid_2d_graph(4,4),first_label=1)
def test_house(self): expected = True g = nx.house_graph() for o in enumerate_dfs_ordering_naively(g): @spec_order(o) def func(g): return is_planar(g) actual = func(g) self.assertEqual(expected, actual)
def test_receive(self): self.s.receive() # check emitting to both neighbors self.assertSetEqual(set(self.s.network.node[0]['agent'].receipts), set([1, 2])) self.assertSetEqual(set(self.s.network.node[0]['agent'].receipts[1]), set()) # emissions of empty graphs should be empty, # but should still have an emissions dictionary self.assertSetEqual(set(self.s.network.node[1]['agent'].receipts[0]), set(nx.house_graph().edges()))
def houseGraphPlotTest(self): """ Creates a simple house graph then draws it to the canvas. This method is intended for testing purposes. :return: """ self.figure.clf() self.the_graph = nx.house_graph() nx.draw(self.the_graph, with_labels=True) self.canvas.draw_idle()
def test_find_jewel(pool : Pool): from jewel import find_jewel for i in range(10): # Bipartite graphs and their complements never have jewels graph = random_bipartite(n1=20, n2=20, p=.5) assert(find_jewel(graph, pool) is None) assert(find_jewel(nx.complement(graph), pool) is None) assert(find_jewel(nx.cycle_graph(4), pool) is None) assert(find_jewel(nx.cycle_graph(5), pool) is not None) assert(find_jewel(nx.cycle_graph(6), pool) is None) assert(find_jewel(nx.petersen_graph(), pool) is not None) assert(find_jewel(nx.house_graph(), pool) is None)
def setUpClass(cls): from social_meaning.society import Society network = nx.star_graph(2) agents = {0: Agent(mental_graph=nx.house_graph(), social_network=network, node_name=0), 1: Agent(mental_graph=nx.Graph(), social_network=network, node_name=1), 2: Agent(mental_graph=nx.Graph(), social_network=network, node_name=2) } methods = {'emit': 'all to neighbors', 'receive': 'all equally', 'update mind': 'union'} cls.s = Society(network, agents, methods)
def generate_subgraph(self, n_nodes_in_subgraph, **kwargs): """ Generate a subgraph with specified properties. Args - n_nodes_in_subgraph (int): number of nodes in each subgraph Return - G (networkx object): subgraph """ subgraph_generator = kwargs.pop('subgraph_generator', 'path') if subgraph_generator == 'cycle': G = nx.cycle_graph(n_nodes_in_subgraph) elif subgraph_generator == 'path': G = nx.path_graph(n_nodes_in_subgraph) elif subgraph_generator == 'house': G = nx.house_graph() elif subgraph_generator == 'complete': G = nx.complete_graph(n_nodes_in_subgraph) elif subgraph_generator == 'star': G = nx.star_graph(n_nodes_in_subgraph) elif subgraph_generator == 'barabasi_albert': m = kwargs.get('m', 5) G = barabasi_albert_graph(n_nodes_in_subgraph, m, seed=config.RANDOM_SEED) elif subgraph_generator == 'extended_barabasi_albert': m = kwargs.get('m', 5) p = kwargs.get('p', 0.5) q = kwargs.get('q', 0) G = extended_barabasi_albert_graph(n_nodes_in_subgraph, m, p, q, seed=config.RANDOM_SEED) elif subgraph_generator == 'duplication_divergence_graph': p = kwargs.get('p', 0.5) G = duplication_divergence_graph(n_nodes_in_subgraph, p) else: raise Exception( 'The subgraph generator you specified is not implemented.') return G
def __init__(self): wx.Frame.__init__(self, None, -1) self.panel = wx.Panel(self) self.fig = plt.figure() self.canvas = FigCanvas(self.panel, -1, self.fig) G=nx.house_graph() pos={0:(0,0), 1:(1,0), 2:(0,1), 3:(1,1), 4:(0.5,2.0)} nx.draw_networkx_nodes(G,pos,node_size=2000,nodelist=[4]) nx.draw_networkx_nodes(G,pos,node_size=3000,nodelist=[0,1,2,3],node_color='b') nx.draw_networkx_edges(G,pos,alpha=0.5,width=6) plt.axis('off') self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) self.toolbar = NavigationToolbar(self.canvas) self.vbox.Add(self.toolbar, 0, wx.EXPAND) self.panel.SetSizer(self.vbox) self.vbox.Fit(self)
def test_line_graph(): """ Tests that `line_graph` produces correct results. """ # Arrange. # Compute the line graph using NX. graph = nx.house_graph() unordered_line_graph = nx.line_graph(graph) # NX puts the nodes in a weird order when making the line graph, # so we sort them to maintain consistency with the original graph and the # incidence matrix. line_graph = nx.Graph() line_graph.add_nodes_from(sorted(unordered_line_graph.nodes(data=True))) line_graph.add_edges_from(unordered_line_graph.edges(data=True)) graph_incidence = nx.incidence_matrix(graph).toarray() true_line_graph_adj = nx.to_numpy_array(line_graph) # Act. got_line_graph_adj = convolution.line_graph(graph_incidence).numpy() # Assert np.testing.assert_array_equal(true_line_graph_adj, got_line_graph_adj)
def test_house_graph(self): print '\nhouse:', self.g = nx.house_graph() a = programming_for_tree_decomposition(self.g, True) self.t = a.tree_decomposition() self.assertTrue(self.__test_all_conditions())
def test_properties_named_small_graphs(self): G = nx.bull_graph() assert G.number_of_nodes() == 5 assert G.number_of_edges() == 5 assert sorted(d for n, d in G.degree()) == [1, 1, 2, 3, 3] assert nx.diameter(G) == 3 assert nx.radius(G) == 2 G = nx.chvatal_graph() assert G.number_of_nodes() == 12 assert G.number_of_edges() == 24 assert list(d for n, d in G.degree()) == 12 * [4] assert nx.diameter(G) == 2 assert nx.radius(G) == 2 G = nx.cubical_graph() assert G.number_of_nodes() == 8 assert G.number_of_edges() == 12 assert list(d for n, d in G.degree()) == 8 * [3] assert nx.diameter(G) == 3 assert nx.radius(G) == 3 G = nx.desargues_graph() assert G.number_of_nodes() == 20 assert G.number_of_edges() == 30 assert list(d for n, d in G.degree()) == 20 * [3] G = nx.diamond_graph() assert G.number_of_nodes() == 4 assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3] assert nx.diameter(G) == 2 assert nx.radius(G) == 1 G = nx.dodecahedral_graph() assert G.number_of_nodes() == 20 assert G.number_of_edges() == 30 assert list(d for n, d in G.degree()) == 20 * [3] assert nx.diameter(G) == 5 assert nx.radius(G) == 5 G = nx.frucht_graph() assert G.number_of_nodes() == 12 assert G.number_of_edges() == 18 assert list(d for n, d in G.degree()) == 12 * [3] assert nx.diameter(G) == 4 assert nx.radius(G) == 3 G = nx.heawood_graph() assert G.number_of_nodes() == 14 assert G.number_of_edges() == 21 assert list(d for n, d in G.degree()) == 14 * [3] assert nx.diameter(G) == 3 assert nx.radius(G) == 3 G = nx.hoffman_singleton_graph() assert G.number_of_nodes() == 50 assert G.number_of_edges() == 175 assert list(d for n, d in G.degree()) == 50 * [7] assert nx.diameter(G) == 2 assert nx.radius(G) == 2 G = nx.house_graph() assert G.number_of_nodes() == 5 assert G.number_of_edges() == 6 assert sorted(d for n, d in G.degree()) == [2, 2, 2, 3, 3] assert nx.diameter(G) == 2 assert nx.radius(G) == 2 G = nx.house_x_graph() assert G.number_of_nodes() == 5 assert G.number_of_edges() == 8 assert sorted(d for n, d in G.degree()) == [2, 3, 3, 4, 4] assert nx.diameter(G) == 2 assert nx.radius(G) == 1 G = nx.icosahedral_graph() assert G.number_of_nodes() == 12 assert G.number_of_edges() == 30 assert (list( d for n, d in G.degree()) == [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]) assert nx.diameter(G) == 3 assert nx.radius(G) == 3 G = nx.krackhardt_kite_graph() assert G.number_of_nodes() == 10 assert G.number_of_edges() == 18 assert (sorted( d for n, d in G.degree()) == [1, 2, 3, 3, 3, 4, 4, 5, 5, 6]) G = nx.moebius_kantor_graph() assert G.number_of_nodes() == 16 assert G.number_of_edges() == 24 assert list(d for n, d in G.degree()) == 16 * [3] assert nx.diameter(G) == 4 G = nx.octahedral_graph() assert G.number_of_nodes() == 6 assert G.number_of_edges() == 12 assert list(d for n, d in G.degree()) == 6 * [4] assert nx.diameter(G) == 2 assert nx.radius(G) == 2 G = nx.pappus_graph() assert G.number_of_nodes() == 18 assert G.number_of_edges() == 27 assert list(d for n, d in G.degree()) == 18 * [3] assert nx.diameter(G) == 4 G = nx.petersen_graph() assert G.number_of_nodes() == 10 assert G.number_of_edges() == 15 assert list(d for n, d in G.degree()) == 10 * [3] assert nx.diameter(G) == 2 assert nx.radius(G) == 2 G = nx.sedgewick_maze_graph() assert G.number_of_nodes() == 8 assert G.number_of_edges() == 10 assert sorted(d for n, d in G.degree()) == [1, 2, 2, 2, 3, 3, 3, 4] G = nx.tetrahedral_graph() assert G.number_of_nodes() == 4 assert G.number_of_edges() == 6 assert list(d for n, d in G.degree()) == [3, 3, 3, 3] assert nx.diameter(G) == 1 assert nx.radius(G) == 1 G = nx.truncated_cube_graph() assert G.number_of_nodes() == 24 assert G.number_of_edges() == 36 assert list(d for n, d in G.degree()) == 24 * [3] G = nx.truncated_tetrahedron_graph() assert G.number_of_nodes() == 12 assert G.number_of_edges() == 18 assert list(d for n, d in G.degree()) == 12 * [3] G = nx.tutte_graph() assert G.number_of_nodes() == 46 assert G.number_of_edges() == 69 assert list(d for n, d in G.degree()) == 46 * [3] # Test create_using with directed or multigraphs on small graphs pytest.raises(nx.NetworkXError, nx.tutte_graph, create_using=nx.DiGraph) MG = nx.tutte_graph(create_using=nx.MultiGraph) assert sorted(MG.edges()) == sorted(G.edges())
for u, v in g.edges(): if 'visited' in g[u][v]: del g[u][v]['visited'] if __name__ == '__main__': targets = { 'bull': nx.bull_graph(), # 1-connected planar 'chvatal': nx.chvatal_graph(), # 4-connected non-planar 'cubical': nx.cubical_graph(), # 3-connected planar 'desargues': nx.desargues_graph(), # 3-connected non-planar 'diamond': nx.diamond_graph(), # 2-connected planar 'dodecahedral': nx.dodecahedral_graph(), # 3-connected planar 'frucht': nx.frucht_graph(), # 3-connected planar 'heawood': nx.heawood_graph(), # 3-connected non-planar 'house': nx.house_graph(), # 2-connected planar 'house_x': nx.house_x_graph(), # 2-connected planar 'icosahedral': nx.icosahedral_graph(), # 5-connected planar 'krackhardt': nx.krackhardt_kite_graph(), # 1-connected planar 'moebius': nx.moebius_kantor_graph(), # non-planar 'octahedral': nx.octahedral_graph(), # 4-connected planar 'pappus': nx.pappus_graph(), # 3-connected non-planar 'petersen': nx.petersen_graph(), # 3-connected non-planar 'sedgewick': nx.sedgewick_maze_graph(), # 1-connected planar 'tetrahedral': nx.tetrahedral_graph(), # 3-connected planar 'truncated_cube': nx.truncated_cube_graph(), # 3-conn. planar 'truncated_tetrahedron': nx.truncated_tetrahedron_graph(), # 3-connected planar 'tutte': nx.tutte_graph() } # 3-connected planar for g_name, g in targets.items():
def small_graphs(): print("Make small graph") G = nx.make_small_graph( ["adjacencylist", "C_4", 4, [[2, 4], [1, 3], [2, 4], [1, 3]]]) draw_graph(G) G = nx.make_small_graph( ["adjacencylist", "C_4", 4, [[2, 4], [3], [4], []]]) draw_graph(G) G = nx.make_small_graph( ["edgelist", "C_4", 4, [[1, 2], [3, 4], [2, 3], [4, 1]]]) draw_graph(G) print("LCF graph") G = nx.LCF_graph(6, [3, -3], 3) draw_graph(G) G = nx.LCF_graph(14, [5, -5], 7) draw_graph(G) print("Bull graph") G = nx.bull_graph() draw_graph(G) print("Chvátal graph") G = nx.chvatal_graph() draw_graph(G) print("Cubical graph") G = nx.cubical_graph() draw_graph(G) print("Desargues graph") G = nx.desargues_graph() draw_graph(G) print("Diamond graph") G = nx.diamond_graph() draw_graph(G) print("Dodechaedral graph") G = nx.dodecahedral_graph() draw_graph(G) print("Frucht graph") G = nx.frucht_graph() draw_graph(G) print("Heawood graph") G = nx.heawood_graph() draw_graph(G) print("House graph") G = nx.house_graph() draw_graph(G) print("House X graph") G = nx.house_x_graph() draw_graph(G) print("Icosahedral graph") G = nx.icosahedral_graph() draw_graph(G) print("Krackhardt kite graph") G = nx.krackhardt_kite_graph() draw_graph(G) print("Moebius kantor graph") G = nx.moebius_kantor_graph() draw_graph(G) print("Octahedral graph") G = nx.octahedral_graph() draw_graph(G) print("Pappus graph") G = nx.pappus_graph() draw_graph(G) print("Petersen graph") G = nx.petersen_graph() draw_graph(G) print("Sedgewick maze graph") G = nx.sedgewick_maze_graph() draw_graph(G) print("Tetrahedral graph") G = nx.tetrahedral_graph() draw_graph(G) print("Truncated cube graph") G = nx.truncated_cube_graph() draw_graph(G) print("Truncated tetrahedron graph") G = nx.truncated_tetrahedron_graph() draw_graph(G) print("Tutte graph") G = nx.tutte_graph() draw_graph(G)
def fmap(results_branch_map, results_gen, results_load_lambda, mapname): G = nx.house_graph() # explicitly set positions pos = {1: (14.370117, 47.418176), 2: (4.614258, 50.699357), 3: (8.041992, 46.820110), 4: (15.424805, 49.856874), 5: (13.051758, 52.447082), 6: (7.690430, 50.754993), 7: (10.415039, 51.799557), 8: (9.494063, 48.693917), 9: (9.448242, 55.940997), 10: (11.645508, 55.245727), 11: (26.264136, 59.256457), 12: (-3.823242, 40.066862), 13: (26.039473, 62.645926), 14: (2.768555, 47.179757), 15: (-1.274414, 52.820469), 16: (22.368164, 38.845412), 17: (19.116211, 47.060143), 18: (10.678711, 44.923325), 19: (11.381836, 43.855656), 20: (12.788086, 42.185115), 21: (16.127930, 40.201253), 22: (14.721680, 37.393436), 23: (9.272461, 39.999566), 24: (23.842208, 55.646382), 25: (5.998535, 49.705536), 26: (25.512130, 57.227828), 27: (5.493164, 51.962320), 28: (10.854492, 61.157371), 29: (7.514648, 59.100786), 30: (10.327148, 63.988811), 31: (16.215820, 68.312733), 32: (7.426758, 61.536660), 33: (19.665527, 52.277120), 34: (-8.217773, 39.729721), 35: (20.170898, 67.010288), 36: (17.182617, 64.674052), 37: (15.073242, 60.471659), 38: (14.018555, 57.104429), 39: (14.699707, 45.991873), 40: (19.489746, 48.787565)} # Calcul of the difference between generation and load diff = [] compteur = 0 for j in range(len(results_load_lambda)): if j != 15 and j != 16 and j != 32 and j != 38 and j != 39: inter = results_gen[compteur, 1] - results_load_lambda[j, 1] diff.append(inter) compteur += 1 diff = np.asarray(diff, dtype=np.float64) # Colors for Exporting and Importing buses compteur = 0 for j in range(len(results_load_lambda)): if j != 15 and j != 16 and j != 32 and j != 38 and j != 39: if diff[compteur] > 0: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j+1], node_color='g') else: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j+1], node_color='r') compteur += 1 else: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j+1], node_color='w') # Colors and width for branches (depending on the capacity used) for i in results_branch_map: u = [i[0], i[1]] ptrans = i[2] pmax = i[3] tau = abs(float(ptrans)/float(pmax)) if tau > 0.8: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1.5, alpha=0.8, edge_color='r') elif 0.5 < tau < 0.8: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1.5, alpha=0.8, edge_color='k') elif 0.25 < tau < 0.5: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1, alpha=0.8, edge_color='b') else: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1, alpha=0.8, edge_color='g') # Names of the buses labels = {} labels[1] = r'$AT$' labels[2] = r'$BE$' labels[3] = r'$CH$' labels[4] = r'$CZ$' labels[5] = r'$DE$' labels[6] = r'$DE$' labels[7] = r'$DE$' labels[8] = r'$DE$' labels[9] = r'$DK$' labels[10] = r'$DK$' labels[11] = r'$EE$' labels[12] = r'$ES$' labels[13] = r'$FI$' labels[14] = r'$FR$' labels[15] = r'$GB$' labels[16] = r'$GR$' labels[17] = r'$HU$' labels[18] = r'$IT$' labels[19] = r'$IT$' labels[20] = r'$IT$' labels[21] = r'$IT$' labels[22] = r'$IT$' labels[23] = r'$IT$' labels[24] = r'$LT$' labels[25] = r'$LU$' labels[26] = r'$LV$' labels[27] = r'$NL$' labels[28] = r'$NO$' labels[29] = r'$NO$' labels[30] = r'$NO$' labels[31] = r'$NO$' labels[32] = r'$NO$' labels[33] = r'$PL$' labels[34] = r'$PT$' labels[35] = r'$SE$' labels[36] = r'$SE$' labels[37] = r'$SE$' labels[38] = r'$SE$' labels[39] = r'$SI$' labels[40] = r'$SK$' # Put labels on each buses # nx.draw_networkx_labels(G, pos, labels, font_size=5) # Background European map map_background = imread('map_europe.png') plt.imshow(map_background, zorder=0, extent=[-25.5, 42, 30.5, 75]) plt.axis('off') plt.savefig(mapname, dpi=1000) # save as png plt.show() # display
def fmap(results_branch_map, results_gen, results_load_lambda, mapname): G = nx.house_graph() # explicitly set positions pos = { 1: (14.370117, 47.418176), 2: (4.614258, 50.699357), 3: (8.041992, 46.820110), 4: (15.424805, 49.856874), 5: (13.051758, 52.447082), 6: (7.690430, 50.754993), 7: (10.415039, 51.799557), 8: (9.494063, 48.693917), 9: (9.448242, 55.940997), 10: (11.645508, 55.245727), 11: (26.264136, 59.256457), 12: (-3.823242, 40.066862), 13: (26.039473, 62.645926), 14: (2.768555, 47.179757), 15: (-1.274414, 52.820469), 16: (22.368164, 38.845412), 17: (19.116211, 47.060143), 18: (10.678711, 44.923325), 19: (11.381836, 43.855656), 20: (12.788086, 42.185115), 21: (16.127930, 40.201253), 22: (14.721680, 37.393436), 23: (9.272461, 39.999566), 24: (23.842208, 55.646382), 25: (5.998535, 49.705536), 26: (25.512130, 57.227828), 27: (5.493164, 51.962320), 28: (10.854492, 61.157371), 29: (7.514648, 59.100786), 30: (10.327148, 63.988811), 31: (16.215820, 68.312733), 32: (7.426758, 61.536660), 33: (19.665527, 52.277120), 34: (-8.217773, 39.729721), 35: (20.170898, 67.010288), 36: (17.182617, 64.674052), 37: (15.073242, 60.471659), 38: (14.018555, 57.104429), 39: (14.699707, 45.991873), 40: (19.489746, 48.787565) } # Calcul of the difference between generation and load diff = [] compteur = 0 for j in range(len(results_load_lambda)): if j != 15 and j != 16 and j != 32 and j != 38 and j != 39: inter = results_gen[compteur, 1] - results_load_lambda[j, 1] diff.append(inter) compteur += 1 diff = np.asarray(diff, dtype=np.float64) # Colors for Exporting and Importing buses compteur = 0 for j in range(len(results_load_lambda)): if j != 15 and j != 16 and j != 32 and j != 38 and j != 39: if diff[compteur] > 0: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j + 1], node_color='g') else: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j + 1], node_color='r') compteur += 1 else: nx.draw_networkx_nodes(G, pos, node_size=30, nodelist=[j + 1], node_color='w') # Colors and width for branches (depending on the capacity used) for i in results_branch_map: u = [i[0], i[1]] ptrans = i[2] pmax = i[3] tau = abs(float(ptrans) / float(pmax)) if tau > 0.8: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1.5, alpha=0.8, edge_color='r') elif 0.5 < tau < 0.8: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1.5, alpha=0.8, edge_color='k') elif 0.25 < tau < 0.5: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1, alpha=0.8, edge_color='b') else: nx.draw_networkx_edges(G, pos, edgelist=[u], width=1, alpha=0.8, edge_color='g') # Names of the buses labels = {} labels[1] = r'$AT$' labels[2] = r'$BE$' labels[3] = r'$CH$' labels[4] = r'$CZ$' labels[5] = r'$DE$' labels[6] = r'$DE$' labels[7] = r'$DE$' labels[8] = r'$DE$' labels[9] = r'$DK$' labels[10] = r'$DK$' labels[11] = r'$EE$' labels[12] = r'$ES$' labels[13] = r'$FI$' labels[14] = r'$FR$' labels[15] = r'$GB$' labels[16] = r'$GR$' labels[17] = r'$HU$' labels[18] = r'$IT$' labels[19] = r'$IT$' labels[20] = r'$IT$' labels[21] = r'$IT$' labels[22] = r'$IT$' labels[23] = r'$IT$' labels[24] = r'$LT$' labels[25] = r'$LU$' labels[26] = r'$LV$' labels[27] = r'$NL$' labels[28] = r'$NO$' labels[29] = r'$NO$' labels[30] = r'$NO$' labels[31] = r'$NO$' labels[32] = r'$NO$' labels[33] = r'$PL$' labels[34] = r'$PT$' labels[35] = r'$SE$' labels[36] = r'$SE$' labels[37] = r'$SE$' labels[38] = r'$SE$' labels[39] = r'$SI$' labels[40] = r'$SK$' # Put labels on each buses # nx.draw_networkx_labels(G, pos, labels, font_size=5) # Background European map map_background = imread('map_europe.png') plt.imshow(map_background, zorder=0, extent=[-25.5, 42, 30.5, 75]) plt.axis('off') plt.savefig(mapname, dpi=1000) # save as png plt.show() # display
import networkx as nx import matplotlib.pyplot as plt import numpy as np import utils G = nx.house_graph() pos = {0: (0, 0), 1: (1, 0), 2: (0, 1), 3: (1, 1), 4: (0.5, 2.0)} adj = nx.adjacency_matrix(G) adj = adj.todense() print('Adjacency') print(adj) N = adj.shape[0] e = np.zeros((N, 1)) e[0, 0] = 1 e[1, 0] = 10 e[2, 0] = 100 e[3, 0] = 1000 e[4, 0] = 10000 # Compute degree matrix using adj matrix # Adj*e = diagnoal entriies of each node o = np.ones((N, 1)) print("Degree") print(np.matmul(adj, o)) print(np.matmul(adj, e)) print(np.matmul(adj, np.identity(N)))
#!/usr/bin/env python """ Draw a graph with matplotlib. You must have matplotlib for this to work. """ # Author: Aric Hagberg ([email protected]) try: import matplotlib.pyplot as plt except: raise import networkx as nx G=nx.house_graph() # explicitly set positions pos={0:(0,0), 1:(1,0), 2:(0,1), 3:(1,1), 4:(0.5,2.0)} nx.draw_networkx_nodes(G,pos,node_size=2000,nodelist=[4]) nx.draw_networkx_nodes(G,pos,node_size=3000,nodelist=[0,1,2,3],node_color='b') nx.draw_networkx_edges(G,pos,alpha=0.5,width=6) plt.axis('off') plt.savefig("house_with_colors.png") # save as png plt.show() # display
import networkx as nx import matplotlib.pylab as plt from plot_multigraph import plot_multigraph graphs = [ ("bull", nx.bull_graph()), ("chvatal", nx.chvatal_graph()), ("cubical", nx.cubical_graph()), ("desargues", nx.desargues_graph()), ("diamond", nx.diamond_graph()), ("dodecahedral", nx.dodecahedral_graph()), ("frucht", nx.frucht_graph()), ("heawood", nx.heawood_graph()), ("house", nx.house_graph()), ("house_x", nx.house_x_graph()), ("icosahedral", nx.icosahedral_graph()), ("krackhardt_kite", nx.krackhardt_kite_graph()), ("moebius_kantor", nx.moebius_kantor_graph()), ("octahedral", nx.octahedral_graph()), ("pappus", nx.pappus_graph()), ("petersen", nx.petersen_graph()), ("sedgewick_maze", nx.sedgewick_maze_graph()), ("tetrahedral", nx.tetrahedral_graph()), ("truncated_cube", nx.truncated_cube_graph()), ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()), ] plot_multigraph(graphs, 4, 5, node_size=50) plt.savefig('graphs/small.png')