畫圖神器pyecharts-?;鶊D

Echarts是一個(gè)由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),得到了眾多開發(fā)者的認(rèn)可。而 Python 是一門富有表達(dá)力的語言,很適合用于數(shù)據(jù)處理。當(dāng)數(shù)據(jù)分析遇上數(shù)據(jù)可視化時(shí),pyecharts 誕生了。
上周畫的旭日圖:畫圖神器pyecharts-旭日圖,閱讀比較多,看來比較受歡迎,今天再介紹個(gè)桑基圖,也是非常好用的圖。
桑基圖(Sankey diagram),即?;芰糠至鲌D,也叫?;芰科胶鈭D。它是一種特定類型的流程圖,圖中延伸的分支的寬度對應(yīng)數(shù)據(jù)流量的大小,通常應(yīng)用于能源、材料成分、金融等數(shù)據(jù)的可視化分析。
一、基本繪圖
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes = [
{"name": "category1"},
{"name": "category2"},
{"name": "category3"},
{"name": "category4"},
{"name": "category5"},
{"name": "category6"},
]
links = [
{"source": "category1", "target": "category2", "value": 10},
{"source": "category2", "target": "category3", "value": 15},
{"source": "category3", "target": "category4", "value": 20},
{"source": "category5", "target": "category6", "value": 25},
]
c = (
Sankey()
.add(
"sankey",
nodes,
links,
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
.render("sankey_base.html")
)
二、復(fù)雜繪圖
import asynciofrom?aiohttp?import?TCPConnector,?ClientSessionimport pyecharts.options as optsfrom pyecharts.charts import Sankey#?獲取官方的數(shù)據(jù),如果下載不了,直接從網(wǎng)站獲取即可https://echarts.apache.org/examples/data/asset/data/energy.jsondata = asyncio.run(get_json_data(url="https://echarts.baidu.com/examples/data/asset/data/energy.json"))(Sankey(init_opts=opts.InitOpts(width="1600px", height="800px")).add(series_name="",nodes=data["nodes"],links=data["links"],itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),linestyle_opt=opts.LineStyleOpts(color="source", curve=0.5, opacity=0.5),tooltip_opts=opts.TooltipOpts(trigger_on="mousemove"),).set_global_opts(title_opts=opts.TitleOpts(title="Sankey Diagram")).render("sankey_diagram.html"))

加群交流學(xué)習(xí)
評論
圖片
表情
