本文整理汇总了Python中networkx.draw_networkx_labels函数的典型用法代码示例。如果您正苦于以下问题:Python draw_networkx_labels函数的具体用法?Python draw_networkx_labels怎么用?Python draw_networkx_labels使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了draw_networkx_labels函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drawgraph
def drawgraph(graph,fixed):
pos=nx.spring_layout(graph)
nx.draw_networkx_nodes(graph,pos,with_labels=True,node_size=100)
nx.draw_networkx_nodes(graph,pos,with_labels=True,nodelist=fixed,node_size=100,node_color="yellow")
nx.draw_networkx_edges(graph,pos,with_labels=True,width=0.3)
nx.draw_networkx_labels(graph,pos,fontsize=10)
plt.show()
示例2: draw_network
def draw_network(self, m=2, pos=None):
edgelist = []
probdict = {}
nodesizes = {}
position = {}
observations = {}
for i, node in enumerate(self.nodes[::-1]):
l, u = node.ppf(.1), node.ppf(.9)
c, w = .5 * (l + u), .5 * (u - l)
edgelist += [(node.name, x.name) for x in node.children]
probdict[node.name] = c
nodesizes[node.name] = 100 + 5000 * w
position[node.name] = (i / m, i % m) if pos is None else pos[node.name]
observations[node.name] = '%.2f+/-%.2f' % (c, w)
G = nx.DiGraph()
G.add_edges_from(edgelist)
values = [probdict.get(node) for node in G.nodes()]
sizes = [nodesizes.get(node) for node in G.nodes()]
plt.figure(figsize=(16,8))
nx.draw_networkx_nodes(G, position, node_size=sizes, cmap=plt.get_cmap('Blues'), node_color=values, vmin=-0.1, vmax=1)
nx.draw_networkx_labels(G, position, {x: x for x in G.nodes()}, font_size=12, font_color='r')
nx.draw_networkx_labels(G, {key: (x[0] , x[1]+.3) for key, x in position.iteritems()}, observations,
font_size=12, font_color='k')
nx.draw_networkx_edges(G, position, edgelist=edgelist, edge_color='r', arrows=True, alpha=0.5)
# plt.xlim((-.15,.9))
plt.show()
示例3: test_labels_and_colors
def test_labels_and_colors(self):
G = nx.cubical_graph()
pos = nx.spring_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos,
nodelist=[0, 1, 2, 3],
node_color='r',
node_size=500,
alpha=0.8)
nx.draw_networkx_nodes(G, pos,
nodelist=[4, 5, 6, 7],
node_color='b',
node_size=500,
alpha=0.8)
# edges
nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
nx.draw_networkx_edges(G, pos,
edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
width=8, alpha=0.5, edge_color='r')
nx.draw_networkx_edges(G, pos,
edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
width=8, alpha=0.5, edge_color='b')
# some math labels
labels = {}
labels[0] = r'$a$'
labels[1] = r'$b$'
labels[2] = r'$c$'
labels[3] = r'$d$'
labels[4] = r'$\alpha$'
labels[5] = r'$\beta$'
labels[6] = r'$\gamma$'
labels[7] = r'$\delta$'
nx.draw_networkx_labels(G, pos, labels, font_size=16)
plt.show()
示例4: plotsolution
def plotsolution(numnodes,coordinates,routes):
plt.ion() # interactive mode on
G=nx.Graph()
nodes = range(1,numnodes+1)
nodedict = {}
for i in nodes:
nodedict[i] = i
nodecolorlist = ['b' for i in nodes]
nodecolorlist[0] = 'r'
# nodes
nx.draw_networkx_nodes(G, coordinates, node_color=nodecolorlist, nodelist=nodes)
# labels
nx.draw_networkx_labels(G,coordinates,font_size=9,font_family='sans-serif',labels = nodedict)
edgelist = defaultdict(list)
colors = ['Navy','PaleVioletRed','Yellow','Darkorange','Chartreuse','CadetBlue','Tomato','Turquoise','Teal','Violet','Silver','LightSeaGreen','DeepPink', 'FireBrick','Blue','Green']
for i in (routes):
edge1 = 1
for j in routes[i][1:]:
edge2 = j
edgelist[i].append((edge1,edge2))
edge1 = edge2
nx.draw_networkx_edges(G,coordinates,edgelist=edgelist[i],
width=6,alpha=0.5,edge_color=colors[i]) #,style='dashed'
plt.savefig("path.png")
plt.show()
示例5: plot
def plot(CG):
"""
Plot the call graph using matplotlib
For larger graphs, this should not be used, as it is very slow
and probably you can not see anything on it.
:param CG: A networkx graph to plot
"""
pos = nx.spring_layout(CG)
internal = []
external = []
for n in CG.node:
if isinstance(n, ExternalMethod):
external.append(n)
else:
internal.append(n)
nx.draw_networkx_nodes(CG, pos=pos, node_color='r', nodelist=internal)
nx.draw_networkx_nodes(CG, pos=pos, node_color='b', nodelist=external)
nx.draw_networkx_edges(CG, pos, arrow=True)
nx.draw_networkx_labels(CG, pos=pos, labels={x: "{} {}".format(x.get_class_name(), x.get_name()) for x in CG.edge})
plt.draw()
plt.show()
示例6: draw_graphs
def draw_graphs(G,edge_pro_dict,Gmp,Ggp,Gadr,Gabm):
plt.figure(1)
pos=nx.spring_layout(G) # positions for all nodes
nx.draw_networkx_nodes(G,pos,noG=800)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos,font_size=10,font_family='sans-serif')
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_pro_dict)
plt.figure(2)
nx.draw_networkx_nodes(Gmp,pos,noG=800)
nx.draw_networkx_edges(Gmp,pos)
nx.draw_networkx_labels(Gmp,pos,font_size=10,font_family='sans-serif')
plt.figure(3)
nx.draw_networkx_nodes(Ggp,pos,noG=800)
nx.draw_networkx_edges(Ggp,pos)
nx.draw_networkx_labels(Ggp,pos,font_size=10,font_family='sans-serif')
plt.figure(4)
nx.draw_networkx_nodes(Gadr,pos,noG=800)
nx.draw_networkx_edges(Gadr,pos)
nx.draw_networkx_labels(Gadr,pos,font_size=10,font_family='sans-serif')
plt.figure(5)
nx.draw_networkx_nodes(Gabm,pos,noG=800)
nx.draw_networkx_edges(Gabm,pos)
nx.draw_networkx_labels(Gabm,pos,font_size=10,font_family='sans-serif')
plt.show()
示例7: draw
def draw(self, layout_type='spring_layout'):
'''
Draw the graph
layout_type = The type of layout algorithm (Default = 'spring_layout')
'''
import matplotlib.pyplot as plt
try:
pos = getattr(nx, layout_type)(self.G)
except:
raise Exception('layout_type of %s is not valid' % layout_type)
nx.draw_networkx_nodes(self.G,pos,
nodelist=self.species,
node_color=self.options['species']['color'],
node_size=self.options['species']['size'],
alpha=self.options['species']['alpha'])
nx.draw_networkx_edges(self.G, pos, edgelist=self.product_edges)
nx.draw_networkx_edges(self.G, pos, edgelist=self.reactant_edges, arrows=False)
nx.draw_networkx_edges(self.G, pos, edgelist=self.modifier_edges, style='dashed')
labels = {}
for n in self.G.nodes():
if n in self.species:
labels[n] = n
nx.draw_networkx_labels(self.G,pos,labels,font_size=self.options['species']['label_size'])
plt.axis('off')
plt.show()
示例8: report_ctg
def report_ctg(ctg, filename):
"""
Reports Clustered Task Graph in the Console and draws CTG in file
:param ctg: clustered task graph
:param filename: drawing file name
:return: None
"""
print "==========================================="
print " REPORTING CLUSTERED TASK GRAPH"
print "==========================================="
cluster_task_list_dict = {}
cluster_weight_dict = {}
for node in ctg.nodes():
print ("\tCLUSTER #: "+str(node)+"\tTASKS:"+str(ctg.node[node]['TaskList'])+"\tUTILIZATION: " +
str(ctg.node[node]['Utilization']))
cluster_task_list_dict[node] = ctg.node[node]['TaskList']
for edge in ctg.edges():
print ("\tEDGE #: "+str(edge)+"\tWEIGHT: "+str(ctg.edge[edge[0]][edge[1]]['Weight']))
cluster_weight_dict[edge] = ctg.edge[edge[0]][edge[1]]['Weight']
print ("PREPARING GRAPH DRAWINGS...")
pos = networkx.shell_layout(ctg)
networkx.draw_networkx_nodes(ctg, pos, node_size=2200, node_color='#FAA5A5')
networkx.draw_networkx_edges(ctg, pos)
networkx.draw_networkx_edge_labels(ctg, pos, edge_labels=cluster_weight_dict)
networkx.draw_networkx_labels(ctg, pos, labels=cluster_task_list_dict)
plt.savefig("GraphDrawings/"+filename)
plt.clf()
print ("\033[35m* VIZ::\033[0mGRAPH DRAWINGS DONE, CHECK \"GraphDrawings/"+filename+"\"")
return None
示例9: draw_graph
def draw_graph(edges, up_words, down_words, node_size, node_color='blue', node_alpha=0.3,
node_text_size=12, edge_color='blue', edge_alpha=0.3, edge_tickness=2,
text_font='sans-serif', file_name="graph"):
plt.clf()
g = nx.Graph()
for edge in edges:
g.add_edge(edge[0], edge[1])
graph_pos = nx.shell_layout(g) # layout for the network
# up_words = map(lambda x: translate_node(x), up_words)
# down_words = map(lambda x: x + "(" + translate_node(x) + ")", down_words) # add translated nodes to graph
try:
nx.draw_networkx_nodes(g, graph_pos, nodelist=up_words, node_size=node_size,
alpha=node_alpha, node_color='red')
nx.draw_networkx_nodes(g, graph_pos, nodelist=down_words, 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)
except:
print 'draw error'
plt.savefig(result_path + file_name, format="PNG")
示例10: plot_dg
def plot_dg(dg):
plt.figure()
# pos = networkx.spring_layout(dg)
pos = networkx.pygraphviz_layout(dg,'dot')
networkx.draw_networkx_labels(dg,pos)
networkx.draw(dg,pos,arrows=True)
plt.show()
示例11: graph_terms_to_topics
def graph_terms_to_topics(lda, num_terms=num_top):
#topic names: select appropriate "t" based on number of topics
#Use line below for num_top = 15.
t = ['0','1', '2','3','4','5','6','7','8','9','10','11','12','13','14']
#Use line below for num_top = 25.
#t = ['0','1', '2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24']
#Use line below for num_top = 35.
#t = ['0','1', '2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34']
#Use line below for num_top = 45.
#t = ['0','1', '2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44']
#Create a network graph and size it.
G = nx.Graph()
plt.figure(figsize=(16,16))
# generate the edges
for i in range(0, lda.num_topics):
topicLabel = t[i]
terms = [term for term, val in lda.show_topic(i, num_terms+1)]
for term in terms:
G.add_edge(topicLabel, term, edge_color='red')
pos = nx.spring_layout(G) # positions for all nodes
#Plot topic labels and terms labels separately to have different colours
g = G.subgraph([topic for topic, _ in pos.items() if topic in t])
nx.draw_networkx_labels(g, pos, font_size=20, font_color='r')
#If network graph is difficult to read, don't plot ngrams titles.
#g = G.subgraph([term for term, _ in pos.items() if str(term) not in t])
#nx.draw_networkx_labels(g, pos, font_size=12, font_color='orange')
#Plot edges
nx.draw_networkx_edges(G, pos, edgelist=G.edges(), alpha=0.3)
#Having trouble saving graph to file automatically; below code not working. Must manually save.
plt.axis('off')
plt.show(block=False)
plt.savefig('/Users/Marcia/OneDrive/UNCC General/DSBA_6880/Misc_Analysis_Files/TopicNetwork'+num+'.png', bbox_inches='tight')
示例12: plot_co_x
def plot_co_x(cox, start, end, size = (20,20), title = '', weighted=False, weight_threshold=10):
""" Plotting function for keyword graphs
Parameters
--------------------
cox: the coword networkx graph; assumes that nodes have attribute 'topic'
start: start year
end: end year
"""
plt.figure(figsize=size)
plt.title(title +' %s - %s'%(start,end), fontsize=18)
if weighted:
elarge=[(u,v) for (u,v,d) in cox.edges(data=True) if d['weight'] >weight_threshold]
esmall=[(u,v) for (u,v,d) in cox.edges(data=True) if d['weight'] <=weight_threshold]
pos=nx.graphviz_layout(cox) # positions for all nodes
nx.draw_networkx_nodes(cox,pos,
node_color= [s*4500 for s in nx.eigenvector_centrality(cox).values()],
node_size = [s*6+20 for s in nx.degree(cox).values()],
alpha=0.7)
# edges
nx.draw_networkx_edges(cox,pos,edgelist=elarge,
width=1, alpha=0.5, edge_color='black') #, edge_cmap=plt.cm.Blues
nx.draw_networkx_edges(cox,pos,edgelist=esmall,
width=0.3,alpha=0.5,edge_color='yellow',style='dotted')
# labels
nx.draw_networkx_labels(cox,pos,font_size=10,font_family='sans-serif')
plt.axis('off')
else:
nx.draw_graphviz(cox, with_labels=True,
alpha = 0.8, width=0.1,
fontsize=9,
node_color = [s*4 for s in nx.eigenvector_centrality(cox).values()],
node_size = [s*6+20 for s in nx.degree(cox).values()])
示例13: draw_graph
def draw_graph(G):
# an example using Graph as a weighted network.
# __author__ = """Aric Hagberg ([email protected])"""
try:
import matplotlib.pyplot as plt
except:
raise
elarge = [(u,v) for (u,v,d) in G.edges(data = True) if d['weight'] > 0.5]
esmall = [(u,v) for (u,v,d) in G.edges(data = True) if d['weight'] <= 0.5]
pos = nx.spring_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos, node_size = 200)
# edges
nx.draw_networkx_edges(G, pos, edgelist = elarge, width = 0.4)
nx.draw_networkx_edges(G, pos, edgelist = esmall, width = 0.4, alpha = 0.6, style = 'dashed')
# labels
nx.draw_networkx_labels(G, pos, font_size = 6, font_family = 'sans-serif')
print 'number of cliques/clusters:', nx.graph_number_of_cliques(G)
print 'time:', time.time() - start
plt.show()
示例14: draw_molecule
def draw_molecule(molecule):
# Create a new NetworkX graph
g = nx.Graph()
# For each vertex and edge in molecule graph add node and edge in NetworkX graph
for n in molecule.vertices():
g.add_node(molecule.position_of_vertex(n), element=n.label)
for e in molecule.edges():
if e.single:
g.add_edge(molecule.endpoints_position(e)[0], molecule.endpoints_position(e)[1], type='single')
elif e.double:
g.add_edge(molecule.endpoints_position(e)[0], molecule.endpoints_position(e)[1], type='double')
elif e.triple:
g.add_edge(molecule.endpoints_position(e)[0], molecule.endpoints_position(e)[1], type='triple')
elif e.quadruple:
g.add_edge(molecule.endpoints_position(e)[0], molecule.endpoints_position(e)[1], type='quadruple')
elif e.aromatic:
g.add_edge(molecule.endpoints_position(e)[0], molecule.endpoints_position(e)[1], type='aromatic')
# Set the layout
pos = nx.spring_layout(g, iterations=30)
# Display the element type and edge type as labels
labels = dict((n,d['element']) for n,d in g.nodes(data=True))
edge_labels = dict(((u,v),d['type']) for u,v,d in g.edges(data=True))
# Add the labels to the graph
nx.draw(g, pos=pos, node_color='w')
nx.draw_networkx_labels(g, pos=pos, labels=labels)
nx.draw_networkx_edge_labels(g, pos=pos, edge_labels=edge_labels)
# Display the completed graph
plt.show()
return g
示例15: plot
def plot(self, show_labels=False):
nodes = nx.draw_networkx_nodes(self,
self.pos,
node_size=NODE_SIZE_NORMAL,
node_color=NODE_COLOR_NORMAL,
linewidths=NODE_LINEWIDTH_NORMAL,
alpha=NODE_ALPHA_NORMAL)
if nodes != None:
nodes.set_edgecolor(NODE_BORDER_COLOR_NORMAL)
ws = nx.get_node_attributes(self, 'w')
sizes = [NODE_SIZE_PHOTO_MIN + ws[v]*NODE_SIZE_PHOTO_SCALE
for v in self.photo_nodes()]
nodes = nx.draw_networkx_nodes(self,
self.pos,
nodelist=self.photo_nodes(),
node_shape=NODE_SHAPE_PHOTO,
node_size=sizes,
node_color=NODE_COLOR_PHOTO)
if nodes != None:
nodes.set_edgecolor(NODE_BORDER_COLOR_PHOTO)
if show_labels:
nx.draw_networkx_labels(self,
self.pos,
font_color=LABEL_COLOR_NORMAL,
font_size=LABEL_FONT_SIZE_NORMAL)
nx.draw_networkx_edges(self,
self.pos,
width=EDGE_WIDTH_NORMAL,
edge_color=EDGE_COLOR_NORMAL,
alpha=EDGE_ALPHA_NORMAL)