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

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

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


总结

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


可视化视角-相关


f6e4b7662193c75ee67be0c96e624f2b_6c7261a3c94c4a889171734c0aabaee9.png

78e15e5844d30434e128e024729c29f2_5354ca8988a74de792450834cffe0e33.png


代码实现

安装依赖

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上以中心放射状分布.
'''

输出为:



2bc49966af9487577823dc2288a8c3de_0b85bf604d7d4880a831bd29a65bf4d2.png

71a9787f8338c7ebb92ad332155a1d3a_700b50b989b5467c9fa41e449c927d13.png

840846cf5f2a6b3976d73a06e830ad69_2e81b4f2c8964566b941b6c38f5dfd3e.png


弧形图

环形弧形长链接图

# -*- 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")

输出为:

a4d9abc86bcbcad9efa9213d3e053288_81f94e51be5c4fe0853dcdba82d785b6.png


桑基图

桑基图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
"""

3272d5c319e0f10c8344ce43c2095f27_e5d36dac2d474a228dee05ea2f273a77.png


桑基图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")

image.png


桑基图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")

9035acc0ddc7dfd1a8771fb35e1f2967_096781913ea94d45be925102fa6dbe06.png


有趣的可视化

https://plotapi.com/#billing_interval

目录
打赏
0
1
1
1
12
分享
相关文章
10 分钟玩转Elastcisearch——数据可视化分析
10 分钟玩转Elastcisearch——数据可视化分析
55 0
数据可视化
【8月更文挑战第9天】数据可视化。
52 2
数据可视化第二版-03部分-12章-网络
数据可视化第二版-03部分-12章-网络
数据可视化第二版-03部分-11章-相关
数据可视化第二版-03部分-11章-相关
ggpubr|让数据可视化更加优雅(上)
ggpubr是由Alboukadel Kassambara创建的,基于ggplot2的可视化包。主要用于绘制符合出版要求的图形。
250 0
ggpubr|让数据可视化更加优雅(上)
ggpubr|让数据可视化更加优雅(下)
ggpubr|让数据可视化更加优雅(下)
362 0
ggpubr|让数据可视化更加优雅(下)
AI助理

阿里云 AI 助理已上线!

快来体验一下吧。