本文整理汇总了Python中networkx.spectral_layout函数的典型用法代码示例。如果您正苦于以下问题:Python spectral_layout函数的具体用法?Python spectral_layout怎么用?Python spectral_layout使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了spectral_layout函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_scale_and_center_arg
def test_scale_and_center_arg(self):
G = nx.complete_graph(9)
G.add_node(9)
vpos = nx.random_layout(G, scale=2, center=(4,5))
self.check_scale_and_center(vpos, scale=2, center=(4,5))
vpos = nx.spring_layout(G, scale=2, center=(4,5))
self.check_scale_and_center(vpos, scale=2, center=(4,5))
vpos = nx.spectral_layout(G, scale=2, center=(4,5))
self.check_scale_and_center(vpos, scale=2, center=(4,5))
# circular can have twice as big length
vpos = nx.circular_layout(G, scale=2, center=(4,5))
self.check_scale_and_center(vpos, scale=2*2, center=(4,5))
vpos = nx.shell_layout(G, scale=2, center=(4,5))
self.check_scale_and_center(vpos, scale=2*2, center=(4,5))
# check default center and scale
vpos = nx.random_layout(G)
self.check_scale_and_center(vpos, scale=1, center=(0.5,0.5))
vpos = nx.spring_layout(G)
self.check_scale_and_center(vpos, scale=1, center=(0.5,0.5))
vpos = nx.spectral_layout(G)
self.check_scale_and_center(vpos, scale=1, center=(0.5,0.5))
vpos = nx.circular_layout(G)
self.check_scale_and_center(vpos, scale=2, center=(0,0))
vpos = nx.shell_layout(G)
self.check_scale_and_center(vpos, scale=2, center=(0,0))
示例2: initialize
def initialize():
G.add_node("A")
G.add_node("B")
G.add_node("C")
G.add_node("D")
G.add_node("E")
G.add_node("F")
labels = {k: k for k in G.nodes()}
G.add_edge("A", "F", weight=1)
G.add_edge("A", "E", weight=3)
G.add_edge("A", "C", weight=4)
G.add_edge("F", "B", weight=3)
G.add_edge("F", "E", weight=2)
G.add_edge("B", "D", weight=3)
G.add_edge("B", "E", weight=2)
G.add_edge("E", "D", weight=4)
G.add_edge("E", "C", weight=2)
G.add_edge("C", "D", weight=1)
labels = nx.get_edge_attributes(G, "weight")
plt.title("Single-Router Network")
nx.draw(G, pos=nx.spectral_layout(G))
nx.draw_networkx(G, with_labels=True, pos=nx.spectral_layout(G))
nx.draw_networkx_edge_labels(G, pos=nx.spectral_layout(G), edge_labels=labels)
plt.show()
示例3: test_smoke_int
def test_smoke_int(self):
G = self.Gi
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.spectral_layout(G)
vpos = nx.spectral_layout(self.bigG)
vpos = nx.shell_layout(G)
示例4: test_smoke_int
def test_smoke_int(self):
G = self.Gi
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.fruchterman_reingold_layout(self.bigG)
vpos = nx.spectral_layout(G)
vpos = nx.spectral_layout(self.bigG)
vpos = nx.shell_layout(G)
if self.scipy is not None:
vpos = nx.kamada_kawai_layout(G)
示例5: test_empty_graph
def test_empty_graph(self):
G=nx.Graph()
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.shell_layout(G)
vpos = nx.spectral_layout(G)
# center arg
vpos = nx.random_layout(G, scale=2, center=(4,5))
vpos = nx.circular_layout(G, scale=2, center=(4,5))
vpos = nx.spring_layout(G, scale=2, center=(4,5))
vpos = nx.shell_layout(G, scale=2, center=(4,5))
vpos = nx.spectral_layout(G, scale=2, center=(4,5))
示例6: test_single_node
def test_single_node(self):
G = nx.Graph()
G.add_node(0)
vpos = nx.random_layout(G)
vpos = nx.circular_layout(G)
vpos = nx.spring_layout(G)
vpos = nx.fruchterman_reingold_layout(G)
vpos = nx.shell_layout(G)
vpos = nx.spectral_layout(G)
# center arg
vpos = nx.random_layout(G, scale=2, center=(4,5))
vpos = nx.circular_layout(G, scale=2, center=(4,5))
vpos = nx.spring_layout(G, scale=2, center=(4,5))
vpos = nx.shell_layout(G, scale=2, center=(4,5))
vpos = nx.spectral_layout(G, scale=2, center=(4,5))
示例7: draw_graph
def draw_graph(graph, labels=None, graph_layout='shell',
node_size=120, node_color='blue', node_alpha=0.3,
node_text_size=8,
edge_color='blue', edge_alpha=0.3, edge_tickness=1,
edge_text_pos=0.3,
text_font='sans-serif'):
# create networkx graph
G=nx.Graph()
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
print G.nodes()
nodeColors = []
nodeClients = []
for node in G.nodes():
if is_number(node):
nodeColors.append(0)
else:
nodeColors.append(1)
nodeClients.append(node)
edgeColors = []
for edge in G.edges():
if (edge[0] in nodeClients) or (edge[1] in nodeClients):
edgeColors.append(1)
else:
edgeColors.append(0)
# these are different layouts for the network you may try
# shell seems to work best
if graph_layout == 'spring':
graph_pos=nx.spring_layout(G)
elif graph_layout == 'spectral':
graph_pos=nx.spectral_layout(G)
elif graph_layout == 'random':
graph_pos=nx.random_layout(G)
else:
graph_pos=nx.shell_layout(G)
# draw graph
nx.draw_networkx_nodes(G,graph_pos,node_size=node_size,
alpha=node_alpha, node_color=nodeColors)
nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,
alpha=edge_alpha,edge_color=edgeColors)
nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size,
font_family=text_font, font_weight='normal', alpha=1.0)
# if labels is None:
# labels = range(len(graph))
# edge_labels = dict(zip(graph, labels))
# nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels,
# label_pos=edge_text_pos)
# show graph
plt.show()
示例8: draw_graph
def draw_graph(G, labels=None, graph_layout='shell',
node_size=1600, node_color='blue', node_alpha=0.3,
node_text_size=12,
edge_color='blue', edge_alpha=0.3, edge_tickness=1,
edge_text_pos=0.3,
text_font='sans-serif'):
# these are different layouts for the network you may try
# shell seems to work best
if graph_layout == 'spring':
graph_pos=nx.spring_layout(G)
elif graph_layout == 'spectral':
graph_pos=nx.spectral_layout(G)
elif graph_layout == 'random':
graph_pos=nx.random_layout(G)
else:
graph_pos=nx.shell_layout(G)
# draw graph
nx.draw_networkx_nodes(G,graph_pos,node_size=node_size,
alpha=node_alpha, node_color=node_color)
nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,
alpha=edge_alpha,edge_color=edge_color)
nx.draw_networkx_labels(G, graph_pos,font_size=node_text_size,
font_family=text_font)
if labels is None:
labels = range(len(G))
edge_labels = dict(zip(G, labels))
nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=labels,
label_pos=edge_text_pos)
# show graph
plt.show()
示例9: setup_space
def setup_space(self):
"""
Method to setup our space.
"""
# Initialize a space with a grid network
self.g = nx.grid_graph(dim=self.size)
self.g=self.g.to_directed()
# Set Pheromones
print 'Setting up network'
capacity_pheromone_list=[self.initial_pheromone]*len(self.capacities[0])*2
capacity_pheromone_list.extend([self.initial_pheromone]*len(self.capacities[1])*2)
for e in self.g.edges_iter():
self.g.add_edge(e[0],e[1],max_capacity=self.edge_capacity)
self.g.add_edge(e[0],e[1],capacity=0) #initial capacity
self.g.add_edge(e[0],e[1],edge_pheromone=[self.initial_pheromone]*2*2) #pheromone per edge
self.g.add_edge(e[0],e[1],capacity_pheromone=capacity_pheromone_list) #pheromone per capacity
for n in self.g.nodes_iter():
neighbors_n=self.g.neighbors(n)
branch_pheromone_list=[]
branch_pheromone_list=[self.initial_pheromone]
branch_pheromone_list.extend([self.initial_pheromone*.5]*(len(neighbors_n)-1))
self.g.add_node(n,branch_pheromone=branch_pheromone_list*2*2)
termination_pheromone_list=[self.initial_termination*0.25,self.initial_termination]*2*2
self.g.add_node(n,termination_pheromone=termination_pheromone_list)
# Set layout
self.g_layout = nx.spectral_layout(self.g)
示例10: draw_graph
def draw_graph(graph, matrix_topology, interfaces_names, color_vector, labels=None, graph_layout='spectral', node_size=600, node_color='blue', node_alpha=0.5,node_text_size=4, edge_color='blue', edge_alpha=0.9, edge_tickness=6,
edge_text_pos=0.25, text_font='sans-serif'):
# create networkx graph
G=nx.Graph()
# add edges
for edge in graph:
G.add_edge(edge[0], edge[1])
# these are different layouts for the network you may try
# spectral seems to work best
if graph_layout == 'spring':
graph_pos=nx.spring_layout(G)
elif graph_layout == 'spectral':
graph_pos=nx.spectral_layout(G)
else:
graph_pos=nx.shell_layout(G)
# draw graph
labels={}
for idx, node in enumerate(G.nodes()):
hostname='R'+str(idx+1)
labels[idx]=hostname
edge_labels=dict(zip(graph, interfaces_names))
nx.draw_networkx_nodes(G,graph_pos,node_size=node_size, alpha=node_alpha, node_color=node_color)
nx.draw_networkx_edges(G,graph_pos,width=edge_tickness, alpha=edge_alpha,edge_color=color_vector)
nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels, label_pos=edge_text_pos, bbox=dict(facecolor='none',edgecolor='none'))
nx.draw_networkx_labels(G, graph_pos, labels, font_size=16)
plt.axis('off')
plt.title('Network topology')
plt.show()
示例11: plot
def plot(self):
"""Visualize the graph."""
try:
import networkx as nx
import matplotlib.pyplot as plt
except Exception as e:
print('Can not plot graph: {}'.format(e))
return
gnx = nx.Graph() if self.is_undirected else nx.DiGraph()
vlbs = {vid: v.vlb for vid, v in self.vertices.items()}
elbs = {}
for vid, v in self.vertices.items():
gnx.add_node(vid, label=v.vlb)
for vid, v in self.vertices.items():
for to, e in v.edges.items():
if (not self.is_undirected) or vid < to:
gnx.add_edge(vid, to, label=e.elb)
elbs[(vid, to)] = e.elb
fsize = (min(16, 1 * len(self.vertices)),
min(16, 1 * len(self.vertices)))
plt.figure(3, figsize=fsize)
pos = nx.spectral_layout(gnx)
nx.draw_networkx(gnx, pos, arrows=True, with_labels=True, labels=vlbs)
nx.draw_networkx_edge_labels(gnx, pos, edge_labels=elbs)
plt.show()
示例12: plot_graphs
def plot_graphs(graphs,t):
num_graphs = len(graphs)
grange = range(0,num_graphs)
for g in grange:
plt.figure(str(g)+str(t))
plt.title("Time " + str(t) + " Graph " + str(g))
edge_labels_1 = nx.get_edge_attributes(graphs[g],'weight')
if g < 3:
nx.draw(graphs[g],nx.spectral_layout(graphs[g],weight=None),with_labels = True)
nx.draw_networkx_edge_labels(graphs[g],nx.spectral_layout(graphs[g],weight=None),edge_labels=edge_labels_1)
else:
nx.draw(graphs[g],nx.spectral_layout(graphs[g],weight=None),with_labels = True)
nx.draw_networkx_edge_labels(graphs[g],nx.spectral_layout(graphs[g],weight=None),edge_labels=edge_labels_1)
plt.savefig("debug_graph_plots/graph_"+str(t)+"_"+str(g)+".pdf")
plt.close()
示例13: prepare_plot
def prepare_plot(graph):
"""
Prepares a Matplotlib plot for further handling
:param graph: datamodel.base.Graph instance
:return: None
"""
G = graph.nxgraph
# Color map for nodes: color is proportional to depth level
# http://matplotlib.org/examples/color/colormaps_reference.html
depth_levels_from_root = nx.shortest_path_length(G, graph.root_node)
vmax = 1.
colormap = plt.get_cmap('BuGn')
step = 1./len(graph)
node_colors = [vmax - step * depth_levels_from_root[n] for n in G.nodes()]
# Draw!
# https://networkx.github.io/documentation/networkx-1.10/reference/drawing.html
pos = nx.spectral_layout(G)
nx.draw_networkx_labels(G, pos,
labels=dict([(n, n.name) for n in G.nodes()]),
font_weight='bold',
font_color='orangered')
nx.draw_networkx_nodes(G, pos,
node_size=2000,
cmap=colormap,
vmin=0.,
vmax=vmax,
node_color=node_colors)
nx.draw_networkx_edge_labels(G, pos,
edge_labels=dict([((u, v,), d['name']) for u, v, d in G.edges(data=True)]))
nx.draw_networkx_edges(G, pos,
edgelist=[edge for edge in G.edges()],
arrows=True)
示例14: draw
def draw(self, layout="graphviz"):
"""
Lay out and draw graph.
Parameters
----------
layout : string ("graphviz")
layout method. Default is "graphviz", which also requires
installation of pygraphviz.
"""
if layout == "graphviz":
pos = nx.graphviz_layout(self.graph)
elif layout == "spring":
pos = nx.spring_layout(self.graph)
elif layout == "spectral":
pos = nx.spectral_layout(self.graph)
elif layout == "circular":
pos = nx.circular_layout(self.graph)
normal = []
msouter = []
minus = []
for node in self.graph.nodes():
if isinstance(node, paths.TISEnsemble):
normal.append(node)
elif isinstance(node, paths.MinusInterfaceEnsemble):
minus.append(node)
else:
msouter.append(node)
nx.draw_networkx_nodes(self.graph, pos, nodelist=normal, node_color="r", node_size=500)
nx.draw_networkx_nodes(self.graph, pos, nodelist=minus, node_color="b", node_size=500)
nx.draw_networkx_nodes(self.graph, pos, nodelist=msouter, node_color="g", node_size=500)
nx.draw_networkx_edges(self.graph, pos, width=self.weights)
示例15: simplePlot
def simplePlot(graph, layout = "shell", nodeSize= 600, widthEdge=2):
""" Plot a directed graph using igraph library.
@type graph: graph
@param graph: a graph to plot
@type layout: string
@param layout: node position method (shell, circular, random, spring, spectral)
"""
G=nx.DiGraph()
for node in graph.keys():
G.add_node(node)
#add edges
for v1 in graph.keys():
for v2 in graph[v1]:
G.add_edge(v1, v2)
# draw graph
if layout == 'circular':
pos = nx.circular_layout(G)
elif layout == 'random':
pos = nx.random_layout(G)
elif layout == 'spring':
pos = nx.random_layout(G)
elif layout == 'spectral':
pos = nx.spectral_layout(G)
else:
pos = nx.shell_layout(G)
nx.draw(G, pos, edge_color='#796d54', alpha=1, node_color='#4370D8',cmap=plt.cm.Blues, node_size=nodeSize, width=widthEdge)
plt.show()