一文速學Python+Pyecharts繪制樹形圖
前言
之前寫pandas和matplotlib的時候說到了想要出一期Pyechart系列數(shù)據(jù)可視化的文章。比起matplotlib,pyeacharts的圖表要豐富而且好看,這取決于它是基于百度團隊使用Javascript開發(fā)的商業(yè)級數(shù)據(jù)圖表。而且pyechart文檔全,便于開發(fā)和閱讀文檔,熟練掌握后是一種非常好用的數(shù)據(jù)可視化的工具之一。當然相比pandas的plot代碼會繁瑣一些,其中一些操作類方法也是比較復雜的,需要對其有個大概的掌握才能作出滿意的圖表。
在我之前的文章中也有好幾次使用到了pyechart方法,但是我覺得既然是完成一些數(shù)據(jù)可視化的操作應該就要快速可呈現(xiàn),作為數(shù)據(jù)處理能夠得到解析出想要的數(shù)據(jù)就足夠了,如果有個業(yè)務小組完全可以將這一部分交給前端去渲染就好了,主要還是快速出圖表給我們自己看,用于調(diào)整代碼而已。那么廢話不多說了開始吧!
一、Tree樹圖
pyecharts只能說不愧是國人開發(fā),文檔真的給力,不用再去啃生肉那么痛苦了。很多詳細的參數(shù)看開發(fā)文檔就可以看明白:pyecharts - A Python Echarts Plotting Library built with love.
我們來看它給出的基礎例圖:
from pyecharts import options as opts
from pyecharts.charts import 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",
}
]
c = (
Tree()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
.render("tree_base.html")
)此代碼會生成一個網(wǎng)頁:

看對應的前端源代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>tree_base.html</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<style type="text/css">
html, body, #container {
height: 100%;
}
body, #container {
overflow: hidden;
margin: 0;
}
#iframe {
width: 100%;
height: 100%;
border: none;
}
</style>
<div id="container">
<iframe id="iframe" sandbox="allow-scripts" src="/files/Hivesqlblood/tree_base.html"></iframe>
</div>
</body>
</html>sandbox="allow-scripts"允許添加腳本執(zhí)行,也就是將我們編寫的python轉(zhuǎn)換為了js腳本,通過代碼輸入端口獲取echart的配置:

如果不想生成網(wǎng)頁將render("tree_base.html")改為render_notebook()即可。
樹形圖有很多種使用場景,比如事件的從屬關系,
這里更主要的是數(shù)據(jù)處理板塊,如果我們僅想要將一行列表數(shù)據(jù)轉(zhuǎn)換為樹形圖數(shù)據(jù)結(jié)構(gòu)該如何處理。
二、數(shù)據(jù)處理
我們拿到展示數(shù)據(jù)結(jié)構(gòu)為:
[ { "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",
}
]也就是結(jié)構(gòu)為 [{"children":[{"name": "B"},{"name": "B"}] ,"name": "temp"}]的類型,我們需要將將數(shù)據(jù)轉(zhuǎn)換為這個格式數(shù)據(jù),就以通用的list來說,如果用樹圖來表示的話肯定是有一節(jié)點為根節(jié)點,一部分節(jié)點為子節(jié)點。就以一個list來說:
list_1=['temp_road_check_20220902', 'dws_crowdsourcing_cs_order_link_mysql', 'track_point_traffic_dev_tk_track_traffic_info_offline']
第一個節(jié)點為根節(jié)點,其余為子節(jié)點。那么我們就可以進行這樣分裝:
list_1=['temp_road_check_20220902', 'dws_crowdsourcing_cs_order_link_mysql', 'track_point_traffic_dev_tk_track_traffic_info_offline']
list_children=[]
for i in range(len(list_1)-1):
children_dict={"name":list_1[i+1]}
list_children.append(children_dict)
dict_children={"children":list_children,"name": list_1[0]}
data=[dict_children]
這樣的話就可以形成樹形圖的格式了:

畫圖也就為:

最好肯定是使用常態(tài)化的思維去封裝這個方法,通過數(shù)據(jù)結(jié)構(gòu)調(diào)整方法。
到此這篇關于一文速學Python+Pyecharts繪制樹形圖的文章就介紹到這了,更多相關Python Pyecharts繪制樹形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Python爬取小姐姐圖片(beautifulsoup法)
這篇文章主要介紹了Python爬取小姐姐圖片(beautifulsoup法),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
pandas數(shù)據(jù)合并與重塑之merge詳解
這篇文章主要介紹了pandas數(shù)據(jù)合并與重塑之merge,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

