1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NetworkX入門(5)

Posted at

ノードを増やす

これまでの2個のノードの場合:

G = nx.DiGraph()
G.add_node(1)
G.add_edge(1, 2)

ノードを増やすのに、add_nodeを使っても良いのだが、そうすると離れ小島ができてしまう。結局、add_nodeとadd_edgeを2つ呼ぶことになる。
その点、add_edgeを使うと、1回でノードとエッジを増やしてくれる。

今回は、add_edgeのリスト版の add_edges_from を使った。引数はエッジのリストである。

G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (2, 4)])

nx.draw_networkx(G, node_color='lightblue', edge_color='dimgray')
plt.savefig('file05_.png')

file05_1.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?