Networkx: указание цветов для сообществ (узлов) на графике - PullRequest
0 голосов
/ 13 июня 2018

Я хочу нарисовать сеть с цветными узлами для каждого обнаруженного сообщества (у меня уже есть списки узлов для каждого сообщества.)

На данный момент у меня есть что-то вроде этого:

plot = nx.draw(G3, nodecolor='r', node_color= 'white', edge_color='k',
         with_labels=True, font_weight='light', node_size= 280, width= 0.9)
plt.savefig('graphe.png')
plt.show()

Как я могу реализовать его таким образом, чтобы каждое сообщество приняло определенный цвет?

1 Ответ

0 голосов
/ 13 июня 2018

Вы были близки.Вам просто нужно нарисовать набор узлов, по одному сообществу за раз.Кроме того, так как мы рисуем это шаг за шагом, мы должны исправить положение узлов.Для этого мы используем nx.spring_layout()

Вот рабочий пример, который поможет вам следовать с фиктивными данными.

node_lists_community1 = [1,2,3, 4]
node_lists_community2 = [5, 6, 7]
node_lists_community3 = [8,10,11,12, 13]
all_nodes = node_lists_community1+ node_lists_community2+ node_lists_community3

#list of edges
elist = [(1,2), (1,3), (2,4), (2, 5), (5,6), (6,7),
        (7,9), (8,10), (8,11), (11,13), (12,13)]

#create the networkx Graph with node types and specifying edge distances
G3 = nx.Graph()
for n in all_nodes:
    G3.add_node(n)
for from_loc, to_loc in elist:
    G3.add_edge(from_loc, to_loc)   

pos = nx.spring_layout(G3) #calculate position for each node
# pos is needed because we are going to draw a few nodes at a time,
# pos fixes their positions.

# Notice that the pos dict is passed to each call to draw below

# Draw the graph, but don't color the nodes
nx.draw(G3, pos, edge_color='k',  with_labels=True,
         font_weight='light', node_size= 280, width= 0.9)

#For each community list, draw the nodes, giving it a specific color.
nx.draw_networkx_nodes(G3, pos, nodelist=node_lists_community1, node_color='b')
nx.draw_networkx_nodes(G3, pos, nodelist=node_lists_community2, node_color='r')
nx.draw_networkx_nodes(G3, pos, nodelist=node_lists_community3, node_color='g')

plt.savefig('graphe.png')
plt.show()

enter image description here

Пожалуйста, спросите, если что-то не ясно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...