python pyecharts繪制矩形樹圖Treemap

@[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("演示數(shù)據(jù)", 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="演示數(shù)據(jù)",
        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()

# 獲取官方的數(shù)據(jù)
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,
        # 標(biāo)簽居中為 position = "inside"
        label_opts=opts.LabelOpts(position="inside"),
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(is_show=False),
        title_opts=opts.TitleOpts(
            title="Echarts 配置項(xiàng)查詢分布", subtitle="2016/04", pos_left="leafDepth"
        ),
    )
    .render("echarts_option_query.html")
)
在這里插入圖片描述
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市转捕,隨后出現(xiàn)的幾起案子作岖,更是在濱河造成了極大的恐慌,老刑警劉巖五芝,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痘儡,死亡現(xiàn)場離奇詭異,居然都是意外死亡枢步,警方通過查閱死者的電腦和手機(jī)沉删,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門渐尿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人矾瑰,你說我怎么就攤上這事砖茸。” “怎么了脯倚?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵渔彰,是天一觀的道長。 經(jīng)常有香客問我推正,道長恍涂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任植榕,我火速辦了婚禮再沧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尊残。我一直安慰自己炒瘸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布寝衫。 她就那樣靜靜地躺著顷扩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪慰毅。 梳的紋絲不亂的頭發(fā)上隘截,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機(jī)與錄音汹胃,去河邊找鬼婶芭。 笑死,一個胖子當(dāng)著我的面吹牛着饥,可吹牛的內(nèi)容都是我干的犀农。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宰掉,長吁一口氣:“原來是場噩夢啊……” “哼呵哨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起轨奄,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤孟害,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后戚绕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡枝冀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年舞丛,在試婚紗的時候發(fā)現(xiàn)自己被綠了耘子。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡球切,死狀恐怖谷誓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吨凑,我是刑警寧澤捍歪,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站鸵钝,受9級特大地震影響糙臼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恩商,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一变逃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧怠堪,春花似錦揽乱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至陌粹,卻和暖如春撒犀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背申屹。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工绘证, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哗讥。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓嚷那,卻偏偏與公主長得像,于是被迫代替她去往敵國和親杆煞。 傳聞我的和親對象是個殘疾皇子魏宽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容