Pyecharts data visualization (3)

Table of contents

1. Draw a word cloud map

2. Draw a Sankey diagram

3. Draw parallel coordinates diagram

4. Draw a node graph

5. Draw a map

This article mainly introduces how to use Pyecharts to draw word cloud diagrams, Sankey diagrams, parallel coordinate diagrams, node diagrams and maps. Although these diagrams are not very commonly used, they still look pretty good. If you put them in the paper, I believe it can make the paper Take it to the next level.

1. Draw a word cloud map

Pyecharts uses WordCloud to draw word cloud graphs.

from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [
    ("牛肉面", 7800),("黄河", 6181),
    ("《读者》杂志", 4386), ("甜胚子", 3055),
    ("甘肃省博物馆", 2055),("莫高窟", 8067),("兰州大学", 4244),
    ("西北师范大学", 1868),("中山桥", 3484),
    ("月牙泉", 1112),("五泉山", 980),
    ("五彩丹霞", 865),("黄河母亲", 847),("崆峒山",678),
    ("羊皮筏子", 1582),("兴隆山",868),
    ("兰州交通大学", 1555),("白塔山", 2550),("五泉山", 2550)]
c = WordCloud()
c.add("", words, word_size_range=[20, 80])
c.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
c.render_notebook()

Result graph:

2. Draw a Sankey diagram

A Sankey Diagram , also known as a Sankey Flow Diagram or a Sankey Energy Diagram, is a type of diagram used to visualize flows, transitions, or relationships. It is mainly composed of nodes (nodes) and edges (links). A node represents an entity or a group of entities, and an edge represents the flow or transfer between nodes. While showing the data flow direction and proportion, the Sankey diagram can clearly present the relationship and interaction between various nodes.

from pyecharts import options as opts
from pyecharts.charts import Sankey
# 节点数据
nodes = [
    {'name': '男生'},
    {'name': '女生'},
    {'name': '打游戏'},
    {'name': '加班'},
    {'name': '追剧'},
]

# 边数据
links = [
    {"source": '男生', "target": '打游戏', "value": 30},
    {"source": '男生', "target": '加班', "value": 20},
    {"source": '女生', "target": '打游戏', "value": 40},
    {"source": '女生', "target": '加班', "value": 15},
    {"source": '女生', "target": '追剧', "value": 25},
]
sankey = (
    Sankey()
    .add(
        "",
        nodes,
        links,
        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
        label_opts=opts.LabelOpts(position="right"),
        node_gap=25
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="男生女生兴趣分布"),
        tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
    )
)
sankey.render_notebook()

Result graph:

3. Draw parallel coordinates diagram

Parallel Coordinates Plot (Parallel Coordinates Plot) is a multidimensional data visualization method for visualizing data sets with multiple numerical variables. It draws multiple coordinate axes on parallel straight lines, each axis represents a variable, and maps each data point to the corresponding position on these coordinate axes to show the relationship and trend among multiple variables.

from pyecharts import options as opts
from pyecharts.charts import Parallel
import numpy as np
import seaborn as sns
data=sns.load_dataset('iris')
data1=np.array(df[['sepal_length','sepal_width','petal_length','petal_width']]).tolist()
parallel_axis=[{"dim":0,"name":"萼片长度"},
               {"dim":1,"name":"萼片宽度"},
               {"dim":2,"name":"花瓣长度"},
               {"dim":3,"name":"花瓣宽度"},
              ]
parallel=Parallel(init_opts=opts.InitOpts(width='600px',height='400px'))
parallel.add_schema(schema=parallel_axis)
parallel.add('iris平行坐标图',data=data1,linestyle_opts=opts.LineStyleOpts(width=4,opacity=0.5))
parallel.render_notebook()

Result graph:

4. Draw a node graph

Node Link Diagram , also known as Network Diagram or Graph, is a graph for visualizing nodes (also known as vertices) and connections between them (also known as edges). chart. Node graphs are often used to represent complex relationships, networks, or systems. In a node graph, a node represents an entity or object, such as a person, a place, an item, etc., and a connection represents a relationship or connection mode between nodes. Connections can be directed or undirected, depending on the nature of the relationship between nodes.

from pyecharts import options as opts
from pyecharts.charts import Graph

nodes = [
    {"name": "A"},
    {"name": "B"},
    {"name": "C"},
    {"name": "D"},
    {"name": "E"},
    {"name": "F"},
]

links = []
for i in range(len(nodes)):
    for j in range(i+1, len(nodes)):
        links.append({"source": nodes[i]["name"], "target": nodes[j]["name"]})

graph = (
    Graph()
    .add("", nodes, links, repulsion=800, layout="force", is_draggable=True)
    .set_global_opts(title_opts=opts.TitleOpts(title="Relationship Graph"))
    .render("relationship_graph.html")
)

Result graph:

5. Draw a map

Draw a map of flight flows in major cities across the country

from pyecharts import options as opts 
from pyecharts.charts import Geo 
from pyecharts.globals import ChartType, SymbolType

c = (
        Geo()
        .add_schema(maptype="china")
        .add( "",
            [ ("哈尔滨", 66), ("重庆", 88), ("上海", 100), ("乌鲁木齐", 30),("北京", 30),("兰州",170)],
            type_=ChartType.EFFECT_SCATTER,
            color="green",
        )
        .add(
            "geo",
            [("北京", "兰州"),( "兰州","北京"), ("重庆", "杭州"),("哈尔滨", "重庆"),("乌鲁木齐", "哈尔滨")],
            type_=ChartType.LINES,
            effect_opts=opts.EffectOpts(
                symbol=SymbolType.ARROW, symbol_size=6, color="blue"
            ),
            linestyle_opts=opts.LineStyleOpts(curve=0.2),
        )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(title_opts=opts.TitleOpts(title="主要城市航班路线和数量"))
    )

c.render_notebook()

Result graph:

Guess you like

Origin blog.csdn.net/m0_64087341/article/details/132642313