Did I find the right examples for you? yes no      Crawl my project      Python Jobs

# AutoNetkit.plot

All Samples(1)  |  Call(1)  |  Derive(0)  |  Import(0)
```Plot the network
```

```        def plot(network, show=False, save=True):
""" Plot the network """
try:
import matplotlib.pyplot as plt
except ImportError:
return
try:
import numpy
except ImportError:
LOG.warn("Matplotlib plotting requires numpy for graph layout")
return

plot_dir = config.plot_dir
if not os.path.isdir(plot_dir):
os.mkdir(plot_dir)

graph = network.graph

try:
#Extract co-ordinates to normalize (needed for PathDrawer, desired for neatness in plots)
x, y = zip(*[(d['x'], d['y']) for n, d in network.graph.nodes(data=True)])
x = numpy.asarray(x, dtype=float)
y = numpy.asarray(y, dtype=float)
#TODO: combine these two operations together
x -= x.min()
x *= 1.0/x.max()
y -= y.min()
y *= -1.0/y.max() # invert
y += 1 # rescale from 0->1 not 1->0
#TODO: see if can use reshape-type commands here
co_ords = zip(list(x), list(y))
co_ords = [numpy.array([x, y]) for x, y in co_ords]
nodes = [n for n in network.graph.nodes()]
pos = dict( zip(nodes, co_ords))
except:
pos=nx.spring_layout(graph)

# Different node color for each AS. Use heatmap based on ASN
paths = []
#paths.append( nx.shortest_path(network.graph, network.find("1a.AS1"), network.find("1c.AS1")))
#paths.append( nx.shortest_path(network.graph, network.find("1b.AS1"), network.find("1c.AS1")))
#paths.append(nx.shortest_path(network.graph, network.find("1a.AS1"), network.find("2c.AS2")))
#paths.append( nx.shortest_path(network.graph, network.find("as100r3.AS100"), network.find("as300r1.AS300")))
#paths.append(nx.shortest_path(network.graph, network.find("as100r2.AS100"), network.find("as30r1.AS30")))

#Node colors
legend = {
'shapes': [],
'labels': [],
}
colormap = cm.jet
unique_asn = sorted(list(set(d.asn for d in network.devices())))
asn_norm = colors.normalize(0, len(unique_asn))

asn_colors = dict.fromkeys(unique_asn)
for index, asn in enumerate(asn_colors.keys()):
asn_color = colormap(asn_norm(index)) #allocate based on index position
asn_colors[asn] = asn_color
legend['shapes'].append( plt.Rectangle((0, 0), 0.51, 0.51,
fc = asn_color))
legend['labels'].append( asn)

node_colors = [asn_colors[device.asn] for device in network.devices()]

plot_graph(graph, title="Network", pos=pos, show=show, save=save,
node_color=node_colors)
return

plot_graph(graph, title="Paths", pos=pos, show=show, save=save,
legend_data = legend,
paths = paths,
node_color=node_colors)

graph = ank.get_ebgp_graph(network)
labels = dict( (n, network.label(n)) for n in graph)
plot_graph(graph, title="eBGP", pos=pos, labels=labels, show=show, save=save)

graph = ank.get_ibgp_graph(network)
labels = dict( (n, network.label(n)) for n in graph)
plot_graph(graph, title="iBGP", pos=pos, labels=labels, show=show, save=save)

graph = ank.get_dns_graph(network)
labels = dict( (n, network.label(n)) for n in graph)
plot_graph(graph, title="DNS", pos=pos, labels=labels, show=show, save=save)
```

```        matplotlib = matplotlib or config.settings['Plotting']['matplotlib']