如何利用pyecharts绘制炫酷的关系网络图?

news/2024/7/7 19:12:55

如何利用pyecharts绘制炫酷的关系网络图

这是本学期在大数据哲学与社会科学实验室做的第六次分享了。

第一次分享的是:

  • 如何利用“wordcloud+jieba”制作中文词云?

第二次分享的是:

  • 如何爬取知乎中问题的回答以及评论的数据?

第三次分享的是:

  • 如何利用百度AI平台或snownlp做中文文本的情感分析?

第四次分享的是:

  • 如何利用情感词典做中文文本的情感分析?

第五次分享的是:

  • 如何利用pyecharts绘制酷炫的桑基图?
  • 如何在Jupyter Lab中显示pyecharts的图形?

本次分享的是“如何利用pyecharts绘制炫酷的关系网络图?”

关系网络图简单来说就是由节点和边连接起来的图,绘制这种图需要用到pyecharts库。

安装:

pip install pyecharts

官方地址:

https://github.com/pyecharts/pyecharts/

开源教程:

https://pyecharts.org/#/zh-cn/intro

关系网络图的节点

class pyecharts.options.GraphNode

class GraphNode(# 数据项名称。name: Optional[str] = None,# 该类目节点标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示宽和高,# 例如 [20, 10] 表示标记宽为 20,高为 10。symbol_size: Union[Numeric, Sequence, None] = None,# 节点的初始 x 值。在不指定的时候需要指明layout属性选择布局方式。x: Optional[Numeric] = None,# 节点的初始 y 值。在不指定的时候需要指明layout属性选择布局方式。y: Optional[Numeric] = None,# 数据项所在类目的 index。category: Optional[int] = None,
)

关系网络图的边

class pyecharts.options.GraphLink

class GraphLink(# 边的源节点名称的字符串,也支持使用数字表示源节点的索引。source: Union[str, int, None] = None,# 边的目标节点名称的字符串,也支持使用数字表示源节点的索引。target: Union[str, int, None] = None,# 边的数值,可以在力引导布局中用于映射到边的长度。value: Optional[Numeric] = None,
)

节点分类的类目

class pyecharts.options.GraphCategory

class GraphCategory(# 类目名称,用于和 legend 对应以及格式化 tooltip 的内容。name: Optional[str] = None,
)    

关系网络图

class pyecharts.charts.Graph

class Graph(# 初始化配置项,参考 `global_options.InitOpts`init_opts: opts.InitOpts = opts.InitOpts()def add(# 系列名称,用于 tooltip 的显示,legend 的图例筛选。series_name: str,# 关系图节点数据项列表,参考 `opts.GraphNode`nodes: Sequence[Union[opts.GraphNode, dict]],# 关系图节点间关系数据项列表,参考 `opts.GraphLink`links: Sequence[Union[opts.GraphLink, dict]],# 关系图节点分类的类目列表,参考 `opts.GraphCategory`categories: Union[Sequence[Union[opts.GraphCategory, dict]], None] = None,# 节点之间的斥力因子。# 支持设置成数组表达斥力的范围,此时不同大小的值会线性映射到不同的斥力。值越大则斥力越大repulsion: Numeric = 50,# 图的布局。可选:# 'none' 不采用任何布局,使用节点中提供的 x, y 作为节点的位置。# 'circular' 采用环形布局。# 'force' 采用力引导布局。layout: str = "force",# 是否开启鼠标缩放和平移漫游。is_roam: bool = True,# 是否在鼠标移到节点上的时候突出显示节点以及节点的边和邻接节点。is_focusnode: bool = True,# 是否旋转标签,默认不旋转。is_rotate_label: bool = False,# 标签配置项,参考 `series_options.LabelOpts`label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),# 关系边的公用线条样式。linestyle_opts: Union[opts.LineStyleOpts, dict] = opts.LineStyleOpts(),)
)

【例子】普通的关系网络图

from pyecharts import options as opts
from pyecharts.charts import Graphnodes = [{'name': '结点1', 'symbolSize': 10},{'name': '结点2', 'symbolSize': 20},{'name': '结点3', 'symbolSize': 30},{'name': '结点4', 'symbolSize': 40},{'name': '结点5', 'symbolSize': 50},{'name': '结点6', 'symbolSize': 40},{'name': '结点7', 'symbolSize': 30},{'name': '结点8', 'symbolSize': 20}
]links = []
for i in nodes:for j in nodes:links.append({'source': i.get('name'), 'target': j.get('name')})G = Graph(init_opts=opts.InitOpts(width='900', height='500'))
G.add(series_name='',nodes=nodes,links=links,repulsion=8000
)
G.set_global_opts(title_opts=opts.TitleOpts(title='Graph-基本示例'))
G.render('graph_base.html')

【例子】带边信息的关系网络图

from pyecharts import options as opts
from pyecharts.charts import Graphnodes = [{'name': '结点1', 'symbolSize': 10},{'name': '结点2', 'symbolSize': 20},{'name': '结点3', 'symbolSize': 30},{'name': '结点4', 'symbolSize': 40},{'name': '结点5', 'symbolSize': 50},{'name': '结点6', 'symbolSize': 60},
]links = [{'source': '结点1', 'target': '结点2', 'value': 2},{'source': '结点2', 'target': '结点3', 'value': 3},{'source': '结点3', 'target': '结点4', 'value': 4},{'source': '结点4', 'target': '结点5', 'value': 5},{'source': '结点5', 'target': '结点6', 'value': 6},{'source': '结点6', 'target': '结点1', 'value': 7},
]G = Graph(init_opts=opts.InitOpts(width='900', height='500'))
G.add(series_name='',nodes=nodes,links=links,repulsion=4000,edge_label=opts.LabelOpts(is_show=True, position='middle', formatter='{b}的数据{c}')
)
G.set_global_opts(title_opts=opts.TitleOpts(title='Graph-带边信息的图'))
G.render('graph_with_edge_options.html')

【例子】NPM中的包依赖

NPM(Node Package Manager)的思路大概是这样的:

1.买个服务器作为代码仓库(registry),在里面放所有需要被共享的代码。

2.发邮件通知 jQuery、Bootstrap、Underscore 作者使用 npm publish 把代码提交到 registry 上,分别取名 jquery、bootstrap 和 underscore(注意大小写)。

3.社区里的其他人如果想使用这些代码,就把 jquery、bootstrap 和 underscore 写到 package.json 里,然后运行 npm install ,npm 就会帮他们下载代码。

4.下载完的代码出现在 node_modules 目录里,可以随意使用了。

这些可以被使用的代码被叫做「包」(package),这就是 NPM 名字的由来:Node Package(包) Manager(管理器)。

import json
from pyecharts import options as opts
from pyecharts.charts import Graphwith open(r'data/npmdepgraph.min10.json', 'r') as f:data = json.load(f)nodes = [{'x': node['x'],'y': node['y'],'name': node['label'],'symbolSize': node['size'],'itemStyle': {'color': node['color']}}for node in data['nodes']
]edges = [{'source': edge['sourceID'],'target': edge['targetID']}for edge in data['edges']
]G = Graph(init_opts=opts.InitOpts(width='900', height='900'))
G.add(series_name='',nodes=nodes,links=edges,layout='none',is_roam=True,is_focusnode=True,label_opts=opts.LabelOpts(is_show=True),linestyle_opts=opts.LineStyleOpts(width=0.5, curve=0.3, opacity=0.7)
)
G.set_global_opts(title_opts=opts.TitleOpts(title='NPM Dependencies'))
G.render('npm_dependencies.html')

【例子】悲惨世界(les miserables)人物关系网络图

import json
from pyecharts import options as opts
from pyecharts.charts import Graphwith open(r'data/les-miserables.json', 'r') as f:data = json.load(f)nodes = []
idDict = dict()
for node in data['nodes']:nodes.append({'x': node['x'],'y': node['y'],'name': node['name'],'symbolSize': node['symbolSize'],'category': node['category'],})idDict[node['id']] = node['name']links = [{'source': idDict[edge['source']],'target': idDict[edge['target']],}for edge in data['links']
]categories = [{'name': category['name']}for category in data['categories']
]G = Graph(init_opts=opts.InitOpts(width='900', height='900'))
G.add(series_name='',nodes=nodes,links=links,categories=categories,layout='circular',is_rotate_label=True,label_opts=opts.LabelOpts(position='right'),linestyle_opts=opts.LineStyleOpts(color='source', curve=0.3, opacity=0.7)
)
G.set_global_opts(title_opts=opts.TitleOpts(title='Les Miserables'),legend_opts=opts.LegendOpts(orient='vertical', pos_left='2%', pos_top='20%')
)
G.render('graph_les_miserables.html')

layout='none'时的图像:

layout='circular'时的图像:

【例子】微博转发关系网络图

import json
from pyecharts import options as opts
from pyecharts.charts import Graphwith open(r'data/weibo.json', 'r',encoding='utf_8_sig') as f:data = json.load(f)nodes = [{'name': node['name'],'symbolSize': node['symbolSize'],'category': node['category'],}for node in data['nodes']
]links = [{'source': edge['source'],'target': edge['target'],}for edge in data['links']
]categories = [{'name': category['name']}for category in data['categories']
]G = Graph(init_opts=opts.InitOpts(width='900', height='900'))
G.add(series_name='',nodes=nodes,links=links,categories=categories,repulsion=30,label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LineStyleOpts(curve=0.2)
)
G.set_global_opts(title_opts=opts.TitleOpts(title='微博转发关系图'),legend_opts=opts.LegendOpts(is_show=False)
)
G.render('graph_with_edge_options.html')


http://lihuaxi.xjx100.cn/news/253194.html

相关文章

HDU 2102 A计划

该题是一道典型的搜索题&#xff0c; #include<stdio.h> #include<stdlib.h> #include<string.h> struct Node {int x, y;int time;int flag; }q[100024]; int d[4][2]{ 0,1,1,0,0,-1,-1,0 }; int N,M; char map[2][13][13]; void getxy( int &X,int &a…

判断链表是否存在环(及其延伸)

有一个单链表&#xff0c;其中可能有一个环&#xff0c;也就是某个节点的next指向的是链表中在它之前的节点&#xff0c;这样在链表的尾部形成一环。问题&#xff1a;1、如何判断一个链表是不是这类链表&#xff1f;2、如果链表为存在环&#xff0c;如果找到环的入口点&#xf…

NCEPU:线下组队学习周报(007)

线下组队学习 经过一段时间的准备&#xff0c;我们组织的线下组队学习逐步进入正轨。欢迎华北电力大学保定校区的伙伴加入进来大家一起学习一起成长。 我们开展组队学习的内容为&#xff1a; &#xff08;1&#xff09;周志华的《机器学习》&#xff08;西瓜书&#xff09; …

谁说 Python 写 GUI 程序丑?那是你不会美化!

作者 | 派森酱来源 | Python技术在平时工作学习当中&#xff0c;我们经常会编写一些简单的 Python GUI 工具&#xff0c;以此来完成各种各样的自动化任务&#xff0c;比如批量处理文件&#xff0c;批量处理图片等等。当我们进行这些工具的编写之时&#xff0c;往往只关注了功能…

Java实现二维码

Java实现二维码 最近突然想写一些博客&#xff0c;所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。 今天这篇博客&#xff0c;主要是利用Java实现二维码&#xff1a; 在写代码之前先讲一下整体思路&#xff0c;以方便更好更快捷的…

【青少年编程】【二级】寻找宝石

「青少年编程竞赛交流群」已成立&#xff08;适合6至18周岁的青少年&#xff09;&#xff0c;公众号后台回复【Scratch】或【Python】&#xff0c;即可进入。如果加入了之前的社群不需要重复加入。 微信后台回复“资料下载”可获取以往学习的材料&#xff08;视频、代码、文档&…

zabbix4.0构建实录

【Nginx】 #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [rootcentos ~]# yum -y install zlib pcre pcre-devel openssl openssl-devel[rootcentos ~]# useradd -s /sbin/nologin nginx [rootzabbix-server ~]# yum install -y nginx 【M…

[原创]Bash中的$*和$@的区别

2019独角兽企业重金招聘Python工程师标准>>> 在Bash脚本中&#xff0c;$*和$都用于表示执行脚本时所传入的参数。先通过一个例子看看他们的区别: #!/bin/bash # testvar.sh echo "-------------ISF is set to \"-seperator\" ------------" IFS…