網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
之前寫(xiě)pandas和matplotlib的時(shí)候說(shuō)到了想要出一期Pyechart系列數(shù)據(jù)可視化的文章。比起matplotlib,pyeacharts的圖表要豐富而且好看,這取決于它是基于百度團(tuán)隊(duì)使用Javascript開(kāi)發(fā)的商業(yè)級(jí)數(shù)據(jù)圖表。而且pyechart文檔全,便于開(kāi)發(fā)和閱讀文檔,熟練掌握后是一種非常好用的數(shù)據(jù)可視化的工具之一。當(dāng)然相比pandas的plot代碼會(huì)繁瑣一些,其中一些操作類方法也是比較復(fù)雜的,需要對(duì)其有個(gè)大概的掌握才能作出滿意的圖表。
在我之前的文章中也有好幾次使用到了pyechart方法,但是我覺(jué)得既然是完成一些數(shù)據(jù)可視化的操作應(yīng)該就要快速可呈現(xiàn),作為數(shù)據(jù)處理能夠得到解析出想要的數(shù)據(jù)就足夠了,如果有個(gè)業(yè)務(wù)小組完全可以將這一部分交給前端去渲染就好了,主要還是快速出圖表給我們自己看,用于調(diào)整代碼而已。那么廢話不多說(shuō)了開(kāi)始吧!
一、Tree樹(shù)圖
pyecharts只能說(shuō)不愧是國(guó)人開(kāi)發(fā),文檔真的給力,不用再去啃生肉那么痛苦了。很多詳細(xì)的參數(shù)看開(kāi)發(fā)文檔就可以看明白:pyecharts - A Python Echarts Plotting Library built with love.
我們來(lái)看它給出的基礎(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")
)
此代碼會(huì)生成一個(gè)網(wǎng)頁(yè):
看對(duì)應(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í)行,也就是將我們編寫(xiě)的python轉(zhuǎn)換為了js腳本,通過(guò)代碼輸入端口獲取echart的配置:
如果不想生成網(wǎng)頁(yè)將render("tree_base.html")改為render_notebook()即可。
樹(shù)形圖有很多種使用場(chǎng)景,比如事件的從屬關(guān)系,
這里更主要的是數(shù)據(jù)處理板塊,如果我們僅想要將一行列表數(shù)據(jù)轉(zhuǎn)換為樹(shù)形圖數(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)換為這個(gè)格式數(shù)據(jù),就以通用的list來(lái)說(shuō),如果用樹(shù)圖來(lái)表示的話肯定是有一節(jié)點(diǎn)為根節(jié)點(diǎn),一部分節(jié)點(diǎn)為子節(jié)點(diǎn)。就以一個(gè)list來(lái)說(shuō):
list_1=['temp_road_check_20220902', 'dws_crowdsourcing_cs_order_link_mysql', 'track_point_traffic_dev_tk_track_traffic_info_offline']
第一個(gè)節(jié)點(diǎn)為根節(jié)點(diǎn),其余為子節(jié)點(diǎn)。那么我們就可以進(jìn)行這樣分裝:
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]
這樣的話就可以形成樹(shù)形圖的格式了:
畫(huà)圖也就為:
最好肯定是使用常態(tài)化的思維去封裝這個(gè)方法,通過(guò)數(shù)據(jù)結(jié)構(gòu)調(diào)整方法。
原文鏈接:https://juejin.cn/post/7158018878327996447
相關(guān)推薦
- 2022-08-05 C語(yǔ)言示例講解if?else語(yǔ)句的用法_C 語(yǔ)言
- 2022-11-19 Python教程之無(wú)限迭代器的使用詳解_python
- 2022-08-15 前端提交代碼時(shí)使用ESLint進(jìn)行規(guī)范校驗(yàn)報(bào)錯(cuò)(Git husky > pre-commit(nod
- 2022-07-29 Golang學(xué)習(xí)之反射機(jī)制的用法詳解_Golang
- 2022-06-29 C++詳細(xì)講解IO流原理_C 語(yǔ)言
- 2022-06-06 Ubuntu系統(tǒng)-FFmpeg安裝及環(huán)境配置
- 2023-02-09 Python去除html標(biāo)簽的幾種方法總結(jié)_python
- 2022-09-09 Nginx使用自簽ssl證書(shū)實(shí)現(xiàn)https連接的方法_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支