Configuring Ubuntu for Python Development

Using Networkx to draw graph

Author posted @ 2014年4月13日 14:51 in Python , 14605 阅读

Draw a directed graph using Networkx 

Code :

try:
    import matplotlib.pyplot as plt
except:
    raise

import networkx as nx

G=nx.DiGraph()
# Add nodes by specifying their positions
G.add_node('10', pos=(2, 10))
G.add_node('9',  pos=(4, 9))
G.add_node('8',  pos=(0, 13))
G.add_node('7',  pos=(1.5, 4))
G.add_node('6',  pos=(4, 4))
G.add_node('5',  pos=(6, 11))
G.add_node('3',  pos=(6, 6))
G.add_node('0',  pos=(0, 0))
# Add edges by defining weight and label
G.add_edge('10','9',weight=1, label='I')
G.add_edge('10','8',weight=1, label='I')
G.add_edge('10','7',weight=1, label='I')
G.add_edge('9','3', weight=1, label='I')
G.add_edge('9','6',weight=1, label='I')
G.add_edge('9','5',weight=1, label='I')
G.add_edge('7','0',weight=1, label='I')
G.add_edge('7','6',weight=0, label='II')
G.add_edge('6','3',weight=0, label='II')
G.add_edge('5','3',weight=0, label='II')

elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.5] # solid edge
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <=0.5] # dashed edge

# Retrieve the positions from graph nodes and save to a dictionary
pos=nx.get_node_attributes(G,'pos')
# Draw nodes
nx.draw_networkx_nodes(G,pos,node_size=700, node_color='orange')

# Draw edges
nx.draw_networkx_edges(G,pos,edgelist=elarge, width=2, edge_color='g')
nx.draw_networkx_edges(G,pos,edgelist=esmall, arrows=False, width=3,
                       alpha=0.5,edge_color='b',style='dashed')

# Draw node labels
nx.draw_networkx_labels(G,pos,font_size=18,font_family='sans-serif')

# Draw edge labels
edge_labels =dict([((u, v), d['label']) 
                   for u, v, d in G.edges(data=True)])

nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

plt.axis('off')
plt.savefig("communication_authority_graph.eps", format='eps') # save as eps
plt.show() # display

References:

  1. draw_networkx_nodes 
  2. draw_networkx_edges
  3. draw_networkx_labels
  4. how to draw graph edges with labels
merckseo 说:
2021年10月10日 06:43

Best Social Plan is the most trusted company for providing social media marketing services. Best Social Plan

seo 说:
2021年10月29日 07:35

I can’t believe that this printing center has the ability to deliver my orders at an amazing speed. The print and the papers were both of high quality. https://www.digitekprinting.com/vinyl-banners this link to know more about their awesome services.

SEO 说:
2022年3月11日 07:17

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. Leedon Green

SEO 说:
2022年4月09日 08:39

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. najbolji brokeri u hrvatskoj

SEO 说:
2022年4月17日 05:32

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... Ki Residences

Pg slot สล็อต เว็บตร 说:
2022年7月14日 17:12

Pg Slot สล็อต เว็บตรง เว็บสล็อตออนไลน์ แตกง่ายจ่ายจริง แจกหนัก แตกหนัง กับ เว็บ pg-slot.game ของเรา ที่กำลังมาแรง แซงเว็บเกมอันดับต้น ๆ ไปหมดแล้ว สมัครสมาชิกได้แล้ววันนี้

NCERT Evs Sample Pap 说:
2022年9月16日 04:08

Environmental Education or Environmental Studies (EVS) was introduced from the foundation of education to enable students to understand the situations occurring around our surroundings without any difficulties. NCERT Evs Sample Paper Class 2 The subject refers to the neighbouring in which all life, also makes use of to solve day-to-day challenges involving the environmental elements.To get a complete analysis of examination question pattern and to get ready to write an exam with confidence, subject experts of the NCERT has provided the Class 2 EVS Sample Paper 2023 with study & learning material that supports all formats of exams held under Term-1, Term-2 and other types of exams for every interested candidate.

NCERT Evs Sample Pap 说:
2022年9月16日 04:10

Environmental Education or Environmental Studies (EVS) was introduced from the foundation of education to enable students to understand the situations occurring around our surroundings without any difficulties. NCERT Evs Sample Paper Class 2 The subject refers to the neighbouring in which all life, also makes use of to solve day-to-day challenges involving the environmental elements.To get a complete analysis of examination question pattern and to get ready to write an exam with confidence, subject experts of the NCERT has provided the Class 2 EVS Sample Paper 2023 with study & learning material that supports all formats of exams held under Term-1, Term-2 and other types of exams for every interested candidate.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter