網(wǎng)站首頁 編程語言 正文
前言
之前寫pandas和matplotlib的時候說到了想要出一期Pyechart系列數(shù)據(jù)可視化的文章。比起matplotlib,pyeacharts的圖表要豐富而且好看,這取決于它是基于百度團隊使用Javascript開發(fā)的商業(yè)級數(shù)據(jù)圖表。而且pyechart文檔全,便于開發(fā)和閱讀文檔,熟練掌握后是一種非常好用的數(shù)據(jù)可視化的工具之一。當(dāng)然相比pandas的plot代碼會繁瑣一些,其中一些操作類方法也是比較復(fù)雜的,需要對其有個大概的掌握才能作出滿意的圖表。
在我之前的文章中也有好幾次使用到了pyechart方法,但是我覺得既然是完成一些數(shù)據(jù)可視化的操作應(yīng)該就要快速可呈現(xiàn),作為數(shù)據(jù)處理能夠得到解析出想要的數(shù)據(jù)就足夠了,如果有個業(yè)務(wù)小組完全可以將這一部分交給前端去渲染就好了,主要還是快速出圖表給我們自己看,用于調(diào)整代碼而已。那么廢話不多說了開始吧!
一、Tree樹圖
pyecharts只能說不愧是國人開發(fā),文檔真的給力,不用再去啃生肉那么痛苦了。很多詳細的參數(shù)看開發(fā)文檔就可以看明白:pyecharts - A Python Echarts Plotting Library built with love.
我們來看它給出的基礎(chǔ)例圖:
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)頁:
看對應(yī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()即可。
樹形圖有很多種使用場景,比如事件的從屬關(guān)系,
這里更主要的是數(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)整方法。
原文鏈接:https://juejin.cn/post/7158018878327996447
相關(guān)推薦
- 2022-05-12 webshell及木馬詳解
- 2022-04-18 python中出現(xiàn)invalid?syntax報錯的幾種原因分析_python
- 2022-08-21 Caffe圖像數(shù)據(jù)轉(zhuǎn)換成可運行l(wèi)eveldb?lmdb文件_其它綜合
- 2022-09-25 線性回歸的從零開始實現(xiàn)(線性神經(jīng)網(wǎng)絡(luò))
- 2022-09-17 Redis實現(xiàn)消息的發(fā)布訂閱原理分析_Redis
- 2022-02-26 Assert.assertEquals()方法參數(shù)詳解_Android
- 2024-07-15 GIT同步修改后的遠程分支
- 2022-07-12 windows系統(tǒng)-串口設(shè)備導(dǎo)致鼠標亂跳的問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支