小白学Python(15)——pyecharts 绘制树形图表 Tree
Tree-基本示例
1 import json 2 import os 3 4 from pyecharts import options as opts 5 from pyecharts.charts import Page, Tree 6 7 data = [ 8 { 9 "children": [ 10 {"name": "B"}, 11 { 12 "children": [ 13 {"children": [{"name": "I"}], "name": "E"}, 14 {"name": "F"}, 15 ], 16 "name": "C", 17 }, 18 { 19 "children": [ 20 {"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, 21 {"name": "H"}, 22 ], 23 "name": "D", 24 }, 25 ], 26 "name": "A", 27 } 28 ] 29 30 tree=( 31 Tree() 32 .add("", data) 33 .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例")) 34 ) 35 36 tree.render()
1 import json 2 import os 3 4 from pyecharts import options as opts 5 from pyecharts.charts import Page, Tree 6 7 data = [ 8 { 9 "children": [ 10 {"name": "B"}, 11 { 12 "children": [ 13 {"children": [{"name": "I"}], "name": "E"}, 14 {"name": "F"}, 15 ], 16 "name": "C", 17 }, 18 { 19 "children": [ 20 {"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, 21 {"name": "H"}, 22 ], 23 "name": "D", 24 }, 25 ], 26 "name": "A", 27 } 28 ] 29 30 31 32 tree=( 33 Tree() 34 .add("", data,orient="RL") 35 .set_global_opts(title_opts=opts.TitleOpts(title="Tree-左右方向")) 36 ) 37 tree.render()
更改orient="TB"/"BT"分别为上下,下上
Tree-Layout
1 import json 2 import os 3 4 from pyecharts import options as opts 5 from pyecharts.charts import Page, Tree 6 7 data = [ 8 { 9 "children": [ 10 {"name": "B"}, 11 { 12 "children": [ 13 {"children": [{"name": "I"}], "name": "E"}, 14 {"name": "F"}, 15 ], 16 "name": "C", 17 }, 18 { 19 "children": [ 20 {"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, 21 {"name": "H"}, 22 ], 23 "name": "D", 24 }, 25 ], 26 "name": "A", 27 } 28 ] 29 30 tree=( 31 Tree() 32 .add("",data,collapse_interval=2, layout="radial") 33 .set_global_opts(title_opts=opts.TitleOpts(title="Tree-Layout")) 34 ) 35 tree.render()
TreeMap-基本示例
1 import json 2 import os 3 4 from pyecharts import options as opts 5 from pyecharts.charts import Page, TreeMap 6 7 data = [ 8 {"value": 40, "name": "我是A"}, 9 { 10 "value": 180, 11 "name": "我是B", 12 "children": [ 13 { 14 "value": 76, 15 "name": "我是B.children", 16 "children": [ 17 {"value": 12, "name": "我是B.children.a"}, 18 {"value": 28, "name": "我是B.children.b"}, 19 {"value": 20, "name": "我是B.children.c"}, 20 {"value": 16, "name": "我是B.children.d"}, 21 ], 22 } 23 ], 24 }, 25 ] 26 27 treemap = ( 28 TreeMap() 29 .add("演示数据", data) 30 .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-基本示例")) 31 ) 32 33 34 treemap.render()