309专供 之 桑葚图

提示:仅供我的舍友学习使用。


第1关:

代码如下(示例):

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Sankey


nodes = [
    {"name": "category1"},
    {"name": "category2"},
    {"name": "category3"},
    {"name": "category4"},
    {"name": "category5"},
    {"name": "category6"},
]

links = [
    {"source": "category1", "target": "category2", "value": 10},
    {"source": "category2", "target": "category3", "value": 15},
    {"source": "category3", "target": "category4", "value": 20},
    {"source": "category5", "target": "category6", "value": 25},
]

def sankey_chart() -> Sankey:
    # ********* Begin *********#  
    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-基本示例"))

 )
    # ********** End **********#
    return sankey

make_snapshot(snapshot, sankey_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, sankey_base(nodes, links).render(), "StandardAnswer/task1/standard_answer_1.png")

第2关:

代码如下(示例):

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Sankey

# ********* Begin *********#
colors = [
    "#67001f",
    "#b2182b",
    "#d6604d",
    "#f4a582",
    "#fddbc7",
    "#d1e5f0",
    "#92c5de",
    "#4393c3",
    "#2166ac",
    "#053061",
]
nodes = [
    {"name":"a"},
    {"name":"b"},
    {"name":"a1"},
    {"name":"b1"},
    {"name":"c"},
    {"name":"e"},

]
links = [
    {"source":"a","target":"a1","value":5},
    {"source":"e","target":"b","value":3},
    {"source":"a","target":"b1","value":3},
    {"source":"b1","target":"a1","value":1},
    {"source":"b1","target":"c","value":2},
    {"source":"b","target":"c","value":1},
]
# ********** End **********#


def sankey_chart() -> Sankey:
    # ********* Begin *********#  
    sankey = (
        Sankey()
        .set_colors(colors)
        .add(
            "sankey",
            nodes=nodes,
            links=links,
            pos_bottom="10%",
            focus_node_adjacency="allEdges",
            orient="vertical",
            linestyle_opt=opts.LineStyleOpts(opacity=0.2,curve=0.5,color="source"),
            label_opts=opts.LabelOpts(position="top"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Sankey-Vertical"),
            tooltip_opts=opts.TooltipOpts(trigger="item",trigger_on="mousemove"),
        )

    )
    # ********** End **********#
    return sankey

make_snapshot(snapshot, sankey_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, sankey_vertical(colors, nodes, links).render(), "StandardAnswer/task2/standard_answer_2.png")

第3关:

代码如下(示例):

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Sankey
import json

with open("product.json", "r", encoding="utf-8") as f:
    j = json.load(f)


def sankey_chart() -> Sankey:
    # ********* Begin *********#  
    sankey = (
        Sankey()
        
        .add(
            "sankey",
            nodes=j["nodes"],
            links=j["links"],
            pos_top="10%",
            focus_node_adjacency=True,
            levels=[
                opts.SankeyLevelsOpts(
                    depth=0,
                    itemstyle_opts=opts.ItemStyleOpts(color="#fbb4ae"),
                    linestyle_opts=opts.LineStyleOpts(color="source",opacity=0.6),
                ),
                
                opts.SankeyLevelsOpts(
                    depth=1,
                    itemstyle_opts=opts.ItemStyleOpts(color="#b3cde3"),
                    linestyle_opts=opts.LineStyleOpts(color="source",opacity=0.6),
                ),
                opts.SankeyLevelsOpts(
                    depth=2,
                    itemstyle_opts=opts.ItemStyleOpts(color="#ccebc5"),
                    linestyle_opts=opts.LineStyleOpts(color="source",opacity=0.6),
                ),
                opts.SankeyLevelsOpts(
                    depth=3,
                    itemstyle_opts=opts.ItemStyleOpts(color="#decbe4"),
                    linestyle_opts=opts.LineStyleOpts(color="source",opacity=0.6),
                ),

            ],
           
            linestyle_opt=opts.LineStyleOpts(curve=0.5),
            
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Sankey-Level Settings"),
            tooltip_opts=opts.TooltipOpts(trigger="item",trigger_on="mousemove"),
        )
    )
    # ********** End **********#
    return sankey

make_snapshot(snapshot, sankey_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, sankey_with_level_setting(j).render(), "StandardAnswer/task3/standard_answer_3.png")

开始你的任务吧,祝你成功!

你可能感兴趣的:(重生之我是挖掘机,python)