欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

pythonpyecharts绘制矩形树图Treemap

时间:2023-05-15
文章目录

treemap_basetreemap_levelsecharts_option_query treemap_base

from pyecharts import options as optsfrom pyecharts.charts import TreeMapdata = [ {"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 jsonfrom pyecharts import options as optsfrom pyecharts.charts import TreeMapwith 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 reimport asynciofrom aiohttp import TCPConnector, ClientSessionimport pyecharts.options as optsfrom pyecharts.charts import TreeMapasync 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"))

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。