目次
NetworkX
前回、ネットワーク作成ライブラリNetworkXでEdgeを矢印にする方法を試してみました。
あわせて読みたい


【NetworkX】Edgeを矢印にする方法[Python]
NetworkX 前回、ネットワーク作成ライブラリNetworkXでNodeとEgdeの色をカラーマップで指定する方法と透明化する方法を試してみました。 今回はEdgeを矢印にしてネット…
今回はNodeの形状を変える方法を試していきましょう。
まずは基本となるプログラムから。
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, with_labels = True)
実行結果

それでは始めていきましょう。
Nodeの形状
Nodeの形状を変えるには「nx.draw」のオプションに「node_shape」を追加します。
そして使用できる形状は「so^>v<dph8」の10種類です。
一つずつ試してみましょう。
node_shape=’s’:四角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='s', with_labels = True)
実行結果

node_shape=’o’:丸
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='o', with_labels = True)
実行結果

node_shape=’^’:三角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='^', with_labels = True)

node_shape=’>’:右向き三角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='>', with_labels = True)

node_shape=’v’:下向き三角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='v', with_labels = True)

node_shape='<‘:左向き三角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='<', with_labels = True)

node_shape=’d’:ダイヤモンド(菱形)
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='d', with_labels = True)

node_shape=’p’:ペンタゴン(五角形)
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='p', with_labels = True)

node_shape=’h’:ヘキサゴン(六角形)
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='h', with_labels = True)

node_shape=’8’:八角形
import networkx as nx
G = nx.Graph()
G.add_node('A')
G.add_node('B')
G.add_node('C')
G.add_node('D')
G.add_node('E')
G.add_edge('A', 'B')
G.add_edge('A', 'C')
G.add_edge('A', 'D')
G.add_edge('B', 'D')
G.add_edge('D', 'E')
nx.draw(G, node_shape='8', with_labels = True)

ということで10種類全部試してみました。
デフォルトは丸なので、使う目的に合わせて、色々と変えてみるのもいいかもしれませんね。
次回はNodeのポジションに関して色々と試していきましょう。
あわせて読みたい


【NetworkX】ポジションでネットワークの形状を指定[Python]
NetworkX 前回、Pythonでネットワーク作成ライブラリNetworkXでNodeの形状を変える方法を試してみました。 これまでにNodeとEdgeの形や色を変える方法は色々試してきた…
ではでは今回はこんな感じで。
コメント