当前位置: 首页 > news >正文

Python生成物理引擎的简单知识图谱

from IPython.display import HTML
from pyvis.network import Network
import networkx as nx
import os# 修复模板路径问题
from pyvis import network as net_module
os.environ["PATH"] += os.pathsep + os.path.dirname(net_module.__file__)# 创建有向图对象
g = nx.DiGraph()# 添加节点(带分类)
nodes = [("2008 Beijing Olympics", "Event"),("IOC", "Organization"),("Beijing", "City"),("China", "Country"),("He Zhenliang", "Person"),("Samaranch", "Person"),("Three Concepts", "Slogan"),("Moscow Vote", "Event"),("Competitor Cities", "Group"),("Olympic Green", "Venue"),("SARS Prevention", "Measure")
]for node, group in nodes:g.add_node(node, group=group, title=node)# 添加边(关系)
edges = [("Beijing", "2008 Beijing Olympics", "Hosts"),("China", "Beijing", "Capital of"),("IOC", "Beijing", "Selected by"),("He Zhenliang", "Beijing", "Advocated for"),("Samaranch", "Beijing", "Announced selection"),("Three Concepts", "Beijing", "Bid theme"),("Moscow Vote", "Beijing", "Winning venue"),("Competitor Cities", "Beijing", "Defeated"),("Olympic Green", "Beijing", "Constructed for"),("SARS Prevention", "Beijing", "Implemented")
]for src, dst, rel in edges:g.add_edge(src, dst, title=rel)# 配置可视化参数(关键修复:添加notebook=True)
net = Network(height="750px",width="100%",bgcolor="white",font_color="black",notebook=True  # 修复Jupyter环境检测
)# 设置节点样式
node_scale = 1.5
groups = {"Event": {"color": "#FF6F61", "size": 25*node_scale},"Organization": {"color": "#6B5B95", "size": 20*node_scale},"City": {"color": "#88B04B", "size": 35*node_scale},"Country": {"color": "#92A8D1", "size": 30*node_scale},"Person": {"color": "#955251", "size": 20*node_scale},"Slogan": {"color": "#B565A7", "size": 18*node_scale},"Group": {"color": "#009B77", "size": 22*node_scale},"Venue": {"color": "#DD4124", "size": 20*node_scale},"Measure": {"color": "#D65076", "size": 20*node_scale}
}# 将网络x数据导入pyvis
net.from_nx(g)# 配置物理布局
net.toggle_physics(True)
net.set_options("""
{"physics": {"barnesHut": {"gravitationalConstant": -5000,"centralGravity": 0.3,"springLength": 200,"springConstant": 0.04,"damping": 0.09,"avoidOverlap": 0.5},"maxVelocity": 50,"minVelocity": 0.1}
}
""")# 设置节点样式
for node in net.nodes:group = node["group"]node.update(groups[group])# 生成可视化文件(使用绝对路径)
output_path = os.path.abspath("beijing_olympics.html")
net.show(output_path)# 在Jupyter中内联显示
try:HTML(filename=output_path)
except:print(f"可视化文件已生成到:{output_path}")print("请在浏览器中打开该文件查看结果")

生成的html文件打开后有物理引擎,可以拉伸,旋转,会缠绕,效果如下:

http://www.xdnf.cn/news/595999.html

相关文章:

  • SOC-ESP32S3部分:6-任务看门狗
  • 101个α因子#18
  • 【Python/Tkinter】实现程序菜单
  • JVM部分内容
  • mybatisplus公共字段自动填充
  • 1.3 任务Task的说明(Xqt)
  • [Linux文件系统] “我的文件在哪?”FHS标准深度解析与核心目录实用指南
  • MVC和MVVM架构的区别
  • sqli-labs——二次注入
  • 常见的慢SQL优化方式
  • strlen和sizeof,const char *、char * const 和char []区别
  • 第二十九天打卡
  • 网络割接的详细流程和关键点
  • 关于常见日志的几种级别和格式
  • 加工生产调度(Johnson算法)
  • vue vite textarea标签按下Shift+Enter 换行输入,只按Enter则提交的实现思路
  • 准备好,开始构建:由 Elasticsearch 向量数据库驱动的 Red Hat OpenShift AI 应用程序
  • 手写ES6 Promise() 相关函数
  • 怎么把https://github.com项目拉到自己的github
  • 在Ubuntu18.04下搭建SadTalker让图片开口说话
  • 第五章:异步幻境 · 时间与数据的秘密
  • STM32之温湿度传感器(DHT11)
  • 纯惯导(INS)的误差来源及其对静态漂移曲线的影响
  • SS928V100(Hi3403V100)----NNN推理引擎,AMCT-ONNX模型压缩量化踩坑记录(LINUX版)
  • 数据指标体系:企业数字化转型的“数字基因“革命
  • Even Split_CodeForces - 1666E分析与解答
  • 【三轴加速度计】QMA6100P数据手册解析
  • 基于RFSOC49DR-16收16发 PCIE4.0 X8 射频采集卡
  • 航电系统之云台检测技术篇
  • 5月22总结