简体   繁体   中英

How can I change the node size in a networkx graph?

I have made a graph. Now I am facing a problem when I want to change the node size.

When I used nx.draw_networkx_nodes(G, pos, node_size=times_list, node_color='Gray', node_shape='o') I get the following error:

ValueError: s must be a scalar, or float array-like with the same size as x and y

I can understand pos size and times_list size are not the same but I am unable to resolve my problem.

Code:

import networkx as nx
import matplotlib.pyplot as plt
number_list = [['8801756091550', '8801319406951'], ['8801756091550', '8801521258169'], ['8801756091550', '8801687371406'], ['8801756091550', '8801711787814'], ['8801756091550', '8801713362903'], ['8801756091550', '8801719241757'], ['8801756091550', '8801721554539'], ['8801756091550', '8801723378200'], ['8801756091550', '8801723910133'], ['8801756091550', '8801731468239'], ['8801756091550', '8801736636044'], ['8801756091550', '8801741822826'], ['8801756091550', '8801748138047'], ['8801756091550', '8801753785140'], ['8801756091550', '8801769093309'], ['8801756091550', '8801777701660'], ['8801756091550', '8801777701826'], ['8801756091550', '8801777720099'], ['8801756091550', '8801777720186'], ['8801756091550', '8801794511789'], ['8801756091550', '8801812166549'], ['8801756091550', '8801844515900'], ['8801756091550', '8801886566666'], ['8801319406951', '8801756091550'], ['8801320000807', '8801756091550'], ['8801320010131', '8801756091550'], ['8801320042222', '8801756091550'], ['8801675700444', '8801756091550'], ['8801709640188', '8801756091550'], ['8801711787814', '8801756091550'], ['8801716104535', '8801756091550'], ['8801719241757', '8801756091550'], ['8801721554539', '8801756091550'], ['8801741822826', '8801756091550'], ['8801746355353', '8801756091550'], ['8801777701660', '8801756091550'], ['8801777701699', '8801756091550'], ['8801777701736', '8801756091550'], ['8801777720099', '8801756091550'], ['8801812166549', '8801756091550'], ['8801911102455', '8801756091550'], ['8801912475990', '8801756091550'], ['8801976296851', '8801756091550']]
times_list = [2000.0, 3000.0, 2000.0, 1000.0, 1000.0, 2000.0, 3000.0, 1000.0, 1000.0, 3000.0, 1000.0, 1000.0, 1000.0, 2000.0, 2000.0, 2000.0, 1000.0, 1000.0, 1000.0, 11000.0, 2000.0, 1000.0, 1000.0, 1000.0, 2000.0, 1000.0, 1000.0, 1000.0, 1000.0, 2000.0, 1000.0, 3000.0, 3000.0, 1000.0, 1000.0, 2000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0]
duration_list = ['116', '132', '145', '61', '186', '99', '569', '79', '19', '221', '129', '17', '49', '88', '72', '37', '32', '74', '42', '952', '932', '60', '196', '15', '101', '32', '117', '104', '123', '13', '46', '76', '140', '12', '17', '58', '103', '47', '37', '54', '71', '254', '128']
G = nx.DiGraph()
fig = plt.subplots(figsize =(32,24))
G.add_edges_from(number_list)
pos=nx.spring_layout(G)
nx.draw_networkx(G,pos)
edge_labels = duration_list

nx.draw_networkx_edge_labels(G,pos,edge_labels=dict(zip(map(tuple,number_list), edge_labels)))
G.size(500)

How can I change the size of the nodes?

You can use this line:

nx.draw_networkx_nodes(G, pos, node_size=times_list[:len(pos)], node_color='Gray', node_shape='o')

The problem you have is that pos has less entries than times_list , therefore you have more labels than nodes, which causes your issue. By taking only the first len(pos) entries from times_list you can resolve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM