Data Visualization Second Edition - Part 03 - Chapter 12 - Networks

Data Visualization Second Edition - Part 03 - Chapter 12 - Networks

Summarize

This series of blogs is a teaching resource blog based on the book "Data Visualization Second Edition". This article is mainly about Chapter 12, which is related to network cases.

Visual Perspective - Correlation

insert image description here

insert image description here

Code

install dependencies

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

Network Diagram

Reference: Application Cases of Building Complex Networks Based on NetworkX

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


The output is:
insert image description here

insert image description here

insert image description here

arc chart

circular arc long link diagram

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

The output is:
insert image description here

Sankey diagram

Sankey Figure 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
"""

insert image description here

Sankey Figure 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")

insert image description here

Sankey Figure 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")

insert image description here

interesting visualization

https://plotapi.com/#billing_interval

Textbook screenshot

insert image description here

Guess you like

Origin blog.csdn.net/m0_38139250/article/details/130378673