美文网首页散文简友广场想法
python pyecharts绘制矩形树图Treemap

python pyecharts绘制矩形树图Treemap

作者: Cache_wood | 来源:发表于2022-03-02 21:17 被阅读0次

    @[toc]

    treemap_base
    from pyecharts import options as opts
    from pyecharts.charts import TreeMap
    
    data = [
        {"value": 40, "name": "我是A"},
        {
            "value": 180,
            "name": "我是B",
            "children": [
                {
                    "value": 76,
                    "name": "我是B.children",
                    "children": [
                        {"value": 12, "name": "我是B.children.a"},
                        {"value": 28, "name": "我是B.children.b"},
                        {"value": 20, "name": "我是B.children.c"},
                        {"value": 16, "name": "我是B.children.d"},
                    ],
                }
            ],
        },
    ]
    
    c = (
        TreeMap()
        .add("演示数据", data)
        .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-基本示例"))
        .render("treemap_base.html")
    )
    
    在这里插入图片描述
    treemap_levels
    import json
    
    from pyecharts import options as opts
    from pyecharts.charts import TreeMap
    
    
    with open("treemap.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    c = (
        TreeMap()
        .add(
            series_name="演示数据",
            data=data,
            levels=[
                opts.TreeMapLevelsOpts(
                    treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                        border_color="#555", border_width=4, gap_width=4
                    )
                ),
                opts.TreeMapLevelsOpts(
                    color_saturation=[0.3, 0.6],
                    treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                        border_color_saturation=0.7, gap_width=2, border_width=2
                    ),
                ),
                opts.TreeMapLevelsOpts(
                    color_saturation=[0.3, 0.5],
                    treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                        border_color_saturation=0.6, gap_width=1
                    ),
                ),
                opts.TreeMapLevelsOpts(color_saturation=[0.3, 0.5]),
            ],
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-Levels-配置"))
        .render("treemap_levels.html")
    )
    
    在这里插入图片描述
    echarts_option_query
    import re
    import asyncio
    from aiohttp import TCPConnector, ClientSession
    
    import pyecharts.options as opts
    from pyecharts.charts import TreeMap
    
    async def get_json_data(url: str) -> dict:
        async with ClientSession(connector=TCPConnector(ssl=False)) as session:
            async with session.get(url=url) as response:
                return await response.json()
    
    # 获取官方的数据
    data = asyncio.run(
        get_json_data(
            url="https://echarts.apache.org/examples/data/asset/data/"
            "ec-option-doc-statistics-201604.json"
        )
    )
    
    tree_map_data: dict = {"children": []}
    
    
    def convert(source, target, base_path: str):
        for key in source:
            if base_path != "":
                path = base_path + "." + key
            else:
                path = key
            if re.match(r"/^\$/", key):
                pass
            else:
                child = {"name": path, "children": []}
                target["children"].append(child)
                if isinstance(source[key], dict):
                    convert(source[key], child, path)
                else:
                    target["value"] = source["$count"]
    
    
    convert(source=data, target=tree_map_data, base_path="")
    
    
    (
        TreeMap(init_opts=opts.InitOpts(width="1280px", height="720px"))
        .add(
            series_name="option",
            data=tree_map_data["children"],
            visual_min=300,
            leaf_depth=1,
            # 标签居中为 position = "inside"
            label_opts=opts.LabelOpts(position="inside"),
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            title_opts=opts.TitleOpts(
                title="Echarts 配置项查询分布", subtitle="2016/04", pos_left="leafDepth"
            ),
        )
        .render("echarts_option_query.html")
    )
    
    在这里插入图片描述

    相关文章

      网友评论

        本文标题:python pyecharts绘制矩形树图Treemap

        本文链接:https://www.haomeiwen.com/subject/aednlrtx.html