数据可视化第二版-03部分-12章-网络

简介: 数据可视化第二版-03部分-12章-网络


数据可视化第二版-03部分-12章-网络

总结

本系列博客为基于《数据可视化第二版》一书的教学资源博客。本文主要是第12章,网络案例相关。

可视化视角-相关

代码实现

安装依赖

pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install seaborn -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tushare==1.2.89 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install mplfinance==0.12.9b7 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyheatmap==0.1.12  -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install networkx==3.1

网络图

参考:基于NetworkX构建复杂网络的应用案例

网络图1-networkx
# 网络图
from matplotlib import pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_nodes_from(["A", "B", "C", "D", "E", "F"])
G.add_edges_from([("A", "B"), ("A", "C"), ("A", "D"), ("B", "C"), ("B", "F"), ("C", "E"), ("D", "F")])
# with_labels是否显示标签,node_size节点大小,node_color节点颜色,node_shape节点形状,alpha透明度,linewidths线条宽度
# 左:跳跃式布局
nx.draw(G, with_labels=True, node_size=400, node_color="skyblue", node_shape="o", alpha=1, width=1, font_size=12,
        font_color="black")
plt.show()
# 中:环形布局
nx.draw(G, with_labels=True, node_size=400, node_color="skyblue", node_shape="o", alpha=1, width=1, font_size=12,
        font_color="black", pos=nx.circular_layout(G))
plt.show()
# 右:随机布局
nx.draw(G, with_labels=True, node_size=400, node_color="skyblue", node_shape="o", alpha=1, width=1, font_size=12,
        font_color="black", pos=nx.random_layout(G))
plt.show()
'''
networkx 画图参数:
- node_size: 指定节点的尺寸大小(默认是300)
- node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如'r'为红色,'b'为绿色等,具体可查看手册),用“数据字典”赋值的时候必须对字典取值(.values())后再赋值
- node_shape: 节点的形状(默认是圆形,用字符串'o'标识,具体可查看手册)
- alpha: 透明度 (默认是1.0,不透明,0为完全透明)
- width: 边的宽度 (默认为1.0)
- edge_color: 边的颜色(默认为黑色)
- style: 边的样式(默认为实现,可选: solid|dashed|dotted,dashdot)
- with_labels: 节点是否带标签(默认为True)
- font_size: 节点标签字体大小 (默认为12)
- font_color: 节点标签字体颜色(默认为黑色)
e.g. nx.draw(G,node_size = 30, with_label = False)
绘制节点的尺寸为30,不带标签的网络图。
布局指定节点排列形式
pos = nx.spring_layout
建立布局,对图进行布局美化,networkx 提供的布局方式有:
- circular_layout:节点在一个圆环上均匀分布
- random_layout:节点随机分布
- shell_layout:节点在同心圆上分布
- spring_layout: 用Fruchterman-Reingold算法排列节点
- spectral_layout:根据图的拉普拉斯特征向量排列节
布局也可用pos参数指定,例如,nx.draw(G, pos = spring_layout(G)) 这样指定了networkx上以中心放射状分布.
'''

输出为:

弧形图

环形弧形长链接图
# -*- coding: utf-8 -*-
"""
@reference
https://gallery.pyecharts.org/#/Graph/graph_les_miserables
https://github.com/pyecharts/pyecharts-gallery/blob/master/Graph/les-miserables.json
"""
import json
from pyecharts import options as opts
from pyecharts.charts import Graph
import os
os.chdir(os.path.dirname(__file__))
with open("les-miserables.json", "r", encoding="utf-8") as f:
    j = json.load(f)
    nodes = j["nodes"]
    links = j["links"]
    categories = j["categories"]
c = (
    Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
    .add(
        "",
        nodes=nodes,
        links=links,
        categories=categories,
        layout="circular",
        is_rotate_label=True,
        linestyle_opts=opts.LineStyleOpts(color="source", curve=0.3),
        label_opts=opts.LabelOpts(position="right"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Graph-Les Miserables"),
        legend_opts=opts.LegendOpts(orient="vertical", pos_left="2%", pos_top="20%"),
    )
    .render("graph_les_miserables.html")
)
import os
os.system("graph_les_miserables.html")

输出为:

桑基图

桑基图1-
# -*- coding: utf-8 -*-
"""
@reference: 
https://pyecharts.org/#/zh-cn/basic_charts?id=sankey%ef%bc%9a%e6%a1%91%e5%9f%ba%e5%9b%be
https://gallery.pyecharts.org/#/Sankey/sankey_base
"""
from pyecharts import options as opts
from pyecharts.charts import Sankey
# 内置主题类型可查看 pyecharts.globals.ThemeType
"""
from pyecharts.globals import ThemeType
"""
nodes = [
    {"name": "category1"},
    {"name": "category2"},
    {"name": "category3"},
    {"name": "category4"},
    {"name": "category5"},
    {"name": "category6"},
]
links = [
    {"source": "category1", "target": "category3", "value": 10},
    {"source": "category1", "target": "category4", "value": 15},
    {"source": "category2", "target": "category3", "value": 10},
    {"source": "category2", "target": "category4", "value": 10},
    {"source": "category3", "target": "category5", "value": 20},
    {"source": "category4", "target": "category5", "value": 10},
    {"source": "category4", "target": "category6", "value": 15},
]
# pyecharts V1 版本开始所有方法均支持链式调用。
sankey = (
    Sankey()  # 试试变换主题:Sankey(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)),参考:进阶话题-定制主题
        .add(
        "sankey",
        nodes,
        links,
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="right"),  # 节点标签位置可选,参考:配置项-系列配置项-标签配置项
    )
        .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
        # 或者直接使用字典参数
        # .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
        .render("sankey_base_2.html")
    # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
    # 也可以传入路径参数,如 Sankey.render("sankey_base.html")
)
import os
os.system("sankey_base_2.html")
# 不习惯链式调用的开发者依旧可以单独调用方法
"""
sankey = Sankey()
sankey.add("sankey",
      nodes,
      links,
      linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
      label_opts=opts.LabelOpts(position="right"),
    )
sankey.set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
sankey.render("sankey_base.html")
"""
# 渲染成图片文件
"""
from pyecharts.render import make_snapshot
# 使用 snapshot-selenium 渲染图片(需安装)
from snapshot_selenium import snapshot
make_snapshot(snapshot, sankey, "sankey_base.png")#sankey为html文件
#snapshot-selenium 报错处理可参考:https://blog.csdn.net/snwang_miss/article/details/117728949
"""

桑基图2-
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes = [
    {"name": "category1"},
    {"name": "category2"},
    {"name": "category3"},
    {"name": "category4"},
    {"name": "category5"},
    {"name": "category6"},
]
links = [
    {"source": "category1", "target": "category3", "value": 10},
    {"source": "category1", "target": "category4", "value": 15},
    {"source": "category2", "target": "category3", "value": 10},
    {"source": "category2", "target": "category4", "value": 10},
    {"source": "category3", "target": "category5", "value": 20},
    {"source": "category4", "target": "category5", "value": 10},
    {"source": "category4", "target": "category6", "value": 15},
]
sankey = (
    Sankey()
    .add(
        "sankey",
        nodes,
        links,
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="right"),#节点标签位置
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
    .render("sankey_base.html")
)
import os
os.system("sankey_base.html")

桑基图3-
from pyecharts import options as opts
from pyecharts.charts import Sankey
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
nodes = [
    {"name": "category1"},
    {"name": "category2"},
    {"name": "category3"},
    {"name": "category4"},
    {"name": "category5"},
    {"name": "category6"},
]
links = [
    {"source": "category1", "target": "category3", "value": 10},
    {"source": "category1", "target": "category4", "value": 15},
    {"source": "category2", "target": "category3", "value": 10},
    {"source": "category2", "target": "category4", "value": 10},
    {"source": "category3", "target": "category5", "value": 20},
    {"source": "category4", "target": "category5", "value": 10},
    {"source": "category4", "target": "category6", "value": 15},
]
sankey_vertical = (
    Sankey(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
    .add(
        "sankey",
        nodes,
        links,
        # Sankey 组件离容器外侧的距离 types.Union[str, types.Numeric]:默认值:pos_left="5%",pos_right="20%",
        pos_left="20%",
        orient="vertical",
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="inside"),#节点标签位置可选,参考:配置项-系列配置项-标签配置项
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-Vertical"))
    .render("sankey_vertical.html")
)
import os
os.system("sankey_vertical.html")

有趣的可视化

https://plotapi.com/#billing_interval

教材截图

相关文章
|
10月前
|
JSON 数据可视化 定位技术
python数据可视化开发(3):使用psutil和socket模块获取电脑系统信息(Mac地址、IP地址、主机名、系统用户、硬盘、CPU、内存、网络)
python数据可视化开发(3):使用psutil和socket模块获取电脑系统信息(Mac地址、IP地址、主机名、系统用户、硬盘、CPU、内存、网络)
203 0
|
8天前
|
网络协议 算法 Linux
【Linux】深入探索:Linux网络调试、追踪与优化
【Linux】深入探索:Linux网络调试、追踪与优化
|
2天前
|
Linux Shell 网络安全
网络安全中Dos和linux常用命令总结
本篇是对网安学习中,常用的命令做一个图文与命令示例,并对一些比较重要的dos和shell命令进行总结,方便自己后续学习进行查询,并希望能够给更多人有一个总结命令和了解命令的地方.
22 5
|
4天前
|
Ubuntu 网络协议 Linux
|
5天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
9 1
|
6天前
|
缓存 网络协议 Linux
Linux 网络命令大全,详细归纳!
【4月更文挑战第24天】
30 3
Linux 网络命令大全,详细归纳!
|
7天前
|
网络协议 JavaScript Linux
Linux常用网络指令(下)
Linux常用网络指令(下)
16 0
|
7天前
|
Linux
Linux常用网络指令(上)
Linux常用网络指令(上)
7 0
|
7天前
|
安全 Linux 网络安全
【专栏】Linux 网络扫描工具:nmap,涨知识的时间到了!
【4月更文挑战第28天】nmap, 开源网络扫描工具,用于探测主机、网络信息,包括开放端口、服务类型、OS等。本文分三部分介绍:1) nmap简介与基本原理;2) 使用方法和高级技巧,如脚本扩展;3) 实际应用,如网络安全评估、系统管理和渗透测试。学习nmap需注意合规性,持续探索新技巧,以提升网络管理与安全能力。一起开始nmap的探索之旅吧!
|
7天前
|
安全 网络协议 Linux
【专栏】一文教你玩转 Linux 的 ping 命令,从此成为 Linux 网络高手
【4月更文挑战第28天】本文详细介绍了Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法。通过ping,用户可测试网络连通性、诊断故障及评估性能。此外,文章还讨论了ping在不同协议、模拟网络环境及与其他命令结合使用时的场景。注意防火墙和网络环境可能影响ping结果,理解错误信息有助于网络问题排查。熟练掌握ping命令,能助你成为Linux网络专家。不断学习和实践,提升网络技能,为构建稳定网络环境贡献力量。