流程图(二)利用python绘制网络图
网络图(Network chart)简介
网络图使用节点和连接线来显示事物之间的连接关系,用于说明实体之间的关系。一般分为定向网络图和非定向网络图。
快速绘制
-
基于pyecharts
from pyecharts import options as opts from pyecharts.charts import Graph nodes = [ { "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")}) c = ( Graph() .add("", nodes, links, repulsion=8000) .set_global_opts(title_opts=opts.TitleOpts(title="基本网络图")) ) c.render_notebook()
-
基于networkx
import pandas as pd import numpy as np import networkx as nx import matplotlib.pyplot as plt # 自定义数据 df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']}) # 构造网络图数据格式 G=nx.from_pandas_edgelist(df, 'from', 'to') # 绘制网络图 nx.draw(G, with_labels=True) plt.show()
定制多样化的网络图
自定义网络图一般是结合使用场景对相关参数进行修改,并辅以其他的绘图知识。参数信息可以通过官网进行查看,其他的绘图知识则更多来源于实战经验,大家不妨将接下来的绘图作为一种学习经验,以便于日后总结。
通过pyecharts绘制多样化的网络图
-
点线网络
import requests import json from pyecharts import options as opts from pyecharts.charts import Graph # 获取官方的数据 url = "https://raw.githubusercontent.com/pyecharts/pyecharts-gallery/master/Graph/weibo.json" response = requests.get(url) j = json.loads(response.text) nodes, links, categories, cont, mid, userl = j c = ( Graph() .add( "", nodes, links, categories, repulsion=50, linestyle_opts=opts.LineStyleOpts(curve=0.2), label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( legend_opts=opts.LegendOpts(is_show=False), title_opts=opts.TitleOpts(title="微博转发关系网络"), ) ) c.render_notebook()
-
球状网络
import pyecharts.options as opts from pyecharts.charts import Graph # 获取官方的数据 url = <