
正文
小白学Python(15)——pyecharts 绘制树形图表 Tree
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Tree-基本示例
import json
import os from pyecharts import options as opts
from pyecharts.charts import Page, Tree data = [
{
"children": [
{"name": "B"},
{
"children": [
{"children": [{"name": "I"}], "name": "E"},
{"name": "F"},
],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
] tree=(
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
) tree.render()

import json
import os from pyecharts import options as opts
from pyecharts.charts import Page, Tree data = [
{
"children": [
{"name": "B"},
{
"children": [
{"children": [{"name": "I"}], "name": "E"},
{"name": "F"},
],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
] tree=(
Tree()
.add("", data,orient="RL")
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-左右方向"))
)
tree.render()

更改orient="TB"/"BT"分别为上下,下上


Tree-Layout
import json
import os from pyecharts import options as opts
from pyecharts.charts import Page, Tree data = [
{
"children": [
{"name": "B"},
{
"children": [
{"children": [{"name": "I"}], "name": "E"},
{"name": "F"},
],
"name": "C",
},
{
"children": [
{"children": [{"name": "J"}, {"name": "K"}], "name": "G"},
{"name": "H"},
],
"name": "D",
},
],
"name": "A",
}
] tree=(
Tree()
.add("",data,collapse_interval=2, layout="radial")
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-Layout"))
)
tree.render()

TreeMap-基本示例
import json
import os from pyecharts import options as opts
from pyecharts.charts import Page, 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"},
],
}
],
},
] treemap = (
TreeMap()
.add("演示数据", data)
.set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-基本示例"))
) treemap.render()








