0% found this document useful (0 votes)
18 views3 pages

GRL Ex 5.ipynb - Colaboratory

Uploaded by

suriya prashanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

GRL Ex 5.ipynb - Colaboratory

Uploaded by

suriya prashanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import networkx as nx

import matplotlib.pyplot as plt


from matplotlib import cm
from matplotlib.collections import LineCollection

# Create a directed graph


G_directed = nx.DiGraph()

# Create a cyclic graph


G_cyclic = nx.cycle_graph(5)

# Create a cubical graph


G_cubical = nx.cubical_graph()

# Create a random geometric graph


G_random_geometric = nx.random_geometric_graph(20, 0.3)

# Create a Barabasi-Albert graph


G_barabasi_albert = nx.barabasi_albert_graph(20, 2)

# Create an ego graph


G_ego = nx.ego_graph(G_random_geometric, 1)

# Create a complete graph with a self-loop


G_complete_self_loop = nx.complete_graph(5)
G_complete_self_loop.add_edge(0, 0)

# Create a weighted graph


G_weighted = nx.Graph()
G_weighted.add_edge(1, 2, weight=4)
G_weighted.add_edge(2, 3, weight=2)
G_weighted.add_edge(3, 1, weight=8)

# Set layouts
layouts = ['circular', 'spring', 'shell', 'kamada_kawai']
G_layouts = {layout: nx.layout.__getattribute__(layout + '_layout') for layout in layouts}

# Set edge colormap


edge_colormap = cm.get_cmap('RdYlBu', len(G_weighted.edges))

# Set node color map


node_colormap = cm.get_cmap('Greens', len(G_weighted))

# Draw and show graphs


for G, graph_name in zip([G_directed, G_cyclic, G_cubical, G_random_geometric,
G_barabasi_albert, G_ego, G_complete_self_loop, G_weighted],
['Directed Graph', 'Cyclic Graph', 'Cubical Graph', 'Random Geome
'Barabasi-Albert Graph', 'Ego Graph', 'Complete Graph (Self Loop
plt.figure(figsize=(8, 6))
pos = G_layouts['spring'](G) # You can change 'spring' to other layout names from the
nx.draw(G, pos, with_labels=True, node_color=node_colormap(range(len(G))), cmap=node_c
edges = nx.draw_networkx_edges(G, pos, edge_color=edge_colormap(range(len(G.edges))),

# Create a ScalarMappable to use for colorbar


edge_sm = cm.ScalarMappable(cmap=edge_colormap)
edge_sm.set_array([]) # An empty array is needed for the ScalarMappable

# Create colorbar
plt.colorbar(edge_sm, label='Edge Weight' if graph_name == 'Weighted Graph' else 'Edge
plt.title(graph_name)
plt.show()
<ipython-input-5-c47363bd71df>:39: MatplotlibDeprecationWarning: The get_cmap fu
edge_colormap = cm.get_cmap('RdYlBu', len(G_weighted.edges))
<ipython-input-5-c47363bd71df>:42: MatplotlibDeprecationWarning: The get_cmap fu
node_colormap = cm.get_cmap('Greens', len(G_weighted))
<ipython-input-5-c47363bd71df>:59: MatplotlibDeprecationWarning: Unable to dete
plt.colorbar(edge_sm, label='Edge Weight' if graph_name == 'Weighted Graph' el

You might also like