ZBLOG

修改代码 import urllib.request import json import networkx as nx import matplotlib.pyplot as plt # 下载JSON数据 url = 'http://i9000.net:8888/sgn/LMS/Visual/8/weibo-2.json' filename = 'weibo-2.json' urllib.request.urlretrieve(url, filename) # 读取JSON数据并解析...

import urllib.request import json import networkx as nx import matplotlib.pyplot as plt

下载JSON数据

url = ‘http://i9000.net:8888/sgn/LMS/Visual/8/weibo-2.json’ filename = ‘weibo-2.json’ urllib.request.urlretrieve(url, filename)

读取JSON数据并解析

with open(filename, ‘r’, encoding=‘utf-8’) as f:

data = json.load(f)

创建有向图对象

G = nx.DiGraph()

添加节点和边

for item in data:

source_uid = item.get('source_uid')  # 使用字典的get方法获取source_uid属性值
retweet_uid = item.get('retweet_uid')  # 使用字典的get方法获取retweet_uid属性值
if source_uid and retweet_uid:
    G.add_edge(source_uid, retweet_uid)  # 添加边

绘制图形

plt.figure(figsize=(12, 8)) pos = nx.spring_layout(G) nx.draw_networkx(G, pos=pos, node_size=10) plt.title(‘Weibo Retweet Network’) plt.axis(‘off’) plt.show()


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=10157

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?