You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.4 KiB
42 lines
1.4 KiB
5 days ago
|
from pyvis.network import Network
|
||
|
import json
|
||
|
import matplotlib.pyplot as plt
|
||
|
import networkx as nx
|
||
|
|
||
|
# 读取JSON知识图谱数据
|
||
|
with open(r'D:\dsWork\dsProject\dsLightRag\Doc\史校长资料\技术一-知识图谱源文件\middle_school_math_graph.json',
|
||
|
encoding='utf-8') as f:
|
||
|
data = json.load(f)
|
||
|
|
||
|
# 创建networkx图
|
||
|
G = nx.Graph()
|
||
|
|
||
|
# 添加节点
|
||
|
for node in data['nodes']:
|
||
|
G.add_node(node['id'], name=node['id'][0:4])
|
||
|
|
||
|
for edge in data['links']:
|
||
|
G.add_edge(edge['source'], edge['target'])
|
||
|
|
||
|
# 绘制知识图谱
|
||
|
plt.rcParams['font.sans-serif'] = ['SimHei']
|
||
|
plt.rcParams['axes.unicode_minus'] = False
|
||
|
plt.figure(figsize=(16, 12), dpi=300) # 增加DPI
|
||
|
|
||
|
# 使用更分散的布局算法
|
||
|
pos = nx.spring_layout(G, k=0.3, iterations=50) # 增加k值和迭代次数
|
||
|
|
||
|
# 减少节点大小并增加透明度
|
||
|
nx.draw_networkx_nodes(G, pos, node_size=20, node_color="skyblue", alpha=0.7)
|
||
|
nx.draw_networkx_edges(G, pos, edge_color="gray", alpha=0.3, width=0.5)
|
||
|
|
||
|
# 优化标签显示
|
||
|
labels = {n: G.nodes[n]['name'] for n in G.nodes()}
|
||
|
nx.draw_networkx_labels(G, pos, labels=labels, font_size=8)
|
||
|
|
||
|
plt.title("华为反馈的知识图谱一", fontsize=20)
|
||
|
plt.axis("off")
|
||
|
plt.tight_layout()
|
||
|
plt.savefig('华为反馈的知识图谱【技术一】.png', format='png', bbox_inches='tight', dpi=300) # 保存为PNG
|
||
|
plt.show()
|