「Python数据可视化AI教程」Treemap(矩形树图)2例,代码&教学视频

文章目录

  • 内容介绍
  • 使用的数据集
  • Demo视频
  • Demo案例
    • 层级矩阵树图
    • 基础矩阵树图

内容介绍

Python在处理各种数据时,利用图表 PyEcharts 将其可视化,提供直观、交互丰富、可高度个性化定制的数据可视化图表。以下是基于 Python3PyEchartsTreemap(矩形树图) 图表可视化Demo的基础代码和视频演示。

代码内容基于「Pyecharts 数据可视化」源码版本 1.7.x ,更新内容会进行标记说明对应版本。

使用的数据集

数据部分使用的是 PyEcharts 自带的数据演示数据字典,这部分的数据是随机进行选取,使用模板时将自己的数据直接替换成对应的内容即可。

本文代码使用的是 Pyecharts 自带的数据集 Faker 数据集。
对数据集不了解的请先阅读 「Python数据可视化AI教程」Pyecharts 中 Faker 数据集说明 。

Demo视频

由于目前在 CSDN 发现个问题,视频都放在这个页面一起加载一起播放会很乱。所以想看教学视频的请点击「Python数据可视化AI教程」Treemap(矩形树图)2例,代码&教学视频

Demo案例

层级矩阵树图

import json

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


with open("data/treemap.json", "r", encoding="utf-8") as f:
    data = json.load(f)
c = (
    TreeMap()
    .add(
        series_name="Demo",
        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-setting"))
#     .render("treemap_levels.html")
)
c.render_notebook()

「Python数据可视化AI教程」Treemap(矩形树图)2例,代码&教学视频_第1张图片

基础矩阵树图

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("Demo", data)
    .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-base"))
#     .render("treemap_base.html")
)
c.render_notebook()

「Python数据可视化AI教程」Treemap(矩形树图)2例,代码&教学视频_第2张图片

你可能感兴趣的:(#,数据可视化,python,数据可视化,数据分析,pyecharts)