如何制作twitter的网络图(Python3)

时间:2017-04-25 16:04:47

标签: python-3.x numpy twitter networkx

我是初学者编程谁试图分析我的Twitter帐户的网络。 我正在写这段代码:

api = twitter.Api(consumer_key = my_consumer_key,
          consumer_secret = my_consumer_secret, 
          access_token_key = my_access_token_key, 
          access_token_secret = my_access_token_secret, 
          input_encoding = "UTF-8",
          sleep_on_rate_limit=True)


friends = api.GetFriends()

G = networkx.Graph()

for friend in friends:
   G.add_edge(myname,friend.screen_name)


for friend in friends[-3:]:
    for user in api.GetFriends(friend.id):
        if user in friends:
            G.add_edge(friend.screen_name,user.screen_name)


pos = spring_layout(G)


draw_networkx_nodes(G, pos, node_size = 100, node_color = 'w')
draw_networkx_edges(G, pos, width = 1)
draw_networkx_labels(G, pos, font_size = 12, font_family = 'sans-
serif', font_color = 'r')

xticks([])
yticks([])
savefig("egonetwork.png") 
show()

我可以获得结果,但由于节点数量巨大,因此非常复杂。因此,我想减少节点(跟随者)的数量,例如,仅5或6个节点。我应该编辑哪个部分? 如果对其进行详细解释,将不胜感激。

1 个答案:

答案 0 :(得分:2)

如果我正确理解您的问题,您希望最多选择5/6个关注者及其互连。

如果是这种情况,我建议您更改for循环,以便最多将五个节点添加到图表中。此外,您需要确保只添加添加到图表中的五个关注者的关注者。

for i in range(min(5, len(friends))): 
   G.add_edge(myname,friends[i].screen_name)
   for user in api.GetFriends(friends[i].id):
        if user in friends[:min(5, len(friends))]:
            G.add_edge(friend.screen_name,user.screen_name)

在第一个for循环中,您最多会将五个关注者添加到图表中。 然后,第二个循环将遍历您刚添加的节点的所有关注者,并将其互连添加到其他四个关注者。