日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

pyecharts的Tab和Legend布局詳情_python

作者:帥帥de三叔 ? 更新時間: 2022-05-06 編程語言

導言:

讀者朋友有時候是不是和我有一樣的困惑,用慣了matplotlibseaborn繪制各種各樣的小圖供自己觀察的時候還算得心應手,但是一旦到了要持續(xù)的大批量繪制一些圖表供非數(shù)據(jù)分析人員長久觀察的時候又覺得吃力,那么有沒有一款第三方python模塊能夠幫我們解決這種困惑呢?答案是肯定的,這就要推薦我們今天的主角——pyecharts。

pyecharts是百度開源的一款第三方繪圖模塊,結合的python語言的簡易性和Echarts的強大繪圖特性,可以用python對其調用,輸出交互性好,精美乖巧且符合審美的圖表,而且還可以輕松的集成到Flask,Django 等主流 Web 框架,方便自己和別人長久可持續(xù)觀看。

一、布局設計思路

拋開數(shù)據(jù)談布局簡直有點天荒夜談,數(shù)據(jù)長什么樣決定了圖表的花容月貌,熟稔自己手里的數(shù)據(jù)才能知自知彼繪制出優(yōu)美的圖表出來,首先看一下我們樣例數(shù)據(jù)長什么樣。

上圖是我們的數(shù)據(jù)表,主要包含的字段有id, flight_date,cargo_type,cargo_weight以及cargo_rate, 其中id類似身份識別號,數(shù)量大約有400個左右,一個id就是一個主體,flight_date是記錄id的時間,單位是日期,cargo_type表示主體承載的貨物類別主要有"A;B", "C;D;E"和 "M"三大類,而cargo_weight和cargo_rate分別表示貨物的重量和價格,這種類型的數(shù)據(jù)是不是像極了我們平時遇到的 各個門店里各類商品每天的銷售數(shù)據(jù)。

知道了數(shù)據(jù)長什么樣子后,我們就可以在草稿紙上畫一畫,比如我希望把cargo_weight和cargo_rate兩者隨著時間的變化而展現(xiàn)出的優(yōu)美走位繪制出來,自然而然,flight_date就作為時間線索橫梗在下面,cargo_weight和cargo_rate畫在橫坐標之上的兩位舞者,為了區(qū)分,可以用柱狀圖繪制cargo_weight, 用曲線繪制cargo_rate,猶如蛟龍在群峰之間蜿蜒向前,為了區(qū)分刻畫cargo_weight和cargo_rate兩者之間不同數(shù)量級,我還需要引入主縱坐標和副縱坐標,用主坐標刻畫cargo_weight的度量,用副坐標刻畫cargo_rate的度量,有了這些基本要素之后,接下來問題的關鍵是怎么把id和cargo_type各放恰當?shù)奈恢茫窟@的確需要動些腦子,考慮到id和cargo_type兩者的數(shù)量,可以把cargo_type作為Tab標簽,而id作為Legend圖例,可以讓觀察者每選定一個主體就能看到這個主體不同cargo_type的歷史上cargo_weight和cargo_rate走勢情況,而且還可以賦予每一個cargo_type一個主體配置。

二、操作實踐

有了藍圖便胸有成竹,下面便是撰寫代碼實現(xiàn)的時候了

import pandas as pd
import pymysql
import pyecharts
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Tab
from pyecharts.globals import ThemeType

con = pymysql.Connect(host='000.00.0.00', user ='***', passwd='******', database='***')
r_sql = "select id, cargo_type, flight_date, cargo_weight, cargo_rate from adm.adm_ifs_rate_order_price order by flight_date asc, voyage desc" #航班訂單數(shù)據(jù)
f_sql = "select concat(flight_no, '-', orac_3airport, '-', dstc_3airport) as id from ods.dm_flt_info where flight_date = date_sub(curdate(), INTERVAL -1 day) order by id asc" #次日航班計劃
raw_data = pd.read_sql(con = con, sql = r_sql) #讀取運單原數(shù)據(jù)
flight_id = pd.read_sql(con = con, sql = f_sql )['id'] #讀取航班計劃
con.close() #關閉鏈接

flight_cargo = raw_data.query("id == @flight_id[0]") #篩選具體航班

cargo_type = ['A;B', 'C;D;E', 'M']
cargo_ab = flight_cargo.query("cargo_type == @cargo_type[0] ")[["flight_date", ?"cargo_weight", ?"cargo_rate"]] #篩選某個貨物類別
cargo_cde = flight_cargo.query("cargo_type == @cargo_type[1] ")[["flight_date", ?"cargo_weight", ?"cargo_rate"]] #篩選某個貨物類別
cargo_m = flight_cargo.query("cargo_type == @cargo_type[2] ")[["flight_date", ?"cargo_weight", ?"cargo_rate"]] #篩選某個貨物類別

def ab_() -> Grid:
? ? bar = (
? ? ? ? Bar()
? ? ? ? .add_xaxis(cargo_ab.flight_date.values.tolist())
? ? ? ? .add_yaxis("運量", cargo_ab.cargo_weight.values.tolist(), yaxis_index=0)

? ? ? ? .set_series_opts(
? ? ? ? ? ? itemstyle_opts=opts.ItemStyleOpts(
? ? ? ? ? ? ? ? opacity=0.5,
? ? ? ? ? ? )
? ? ? ? ?)
? ? ? ? ?.extend_axis(
? ? ? ? ? ? yaxis=opts.AxisOpts(
? ? ? ? ? ? ? ? type_="value",
? ? ? ? ? ? ? ? name="運價",
? ? ? ? ? ? ? ? position="right",
? ? ? ? ? ? ? ? axisline_opts=opts.AxisLineOpts(
? ? ? ? ? ? ? ? ? ? linestyle_opts=opts.LineStyleOpts(color="#675bba")
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? splitline_opts=opts.SplitLineOpts(
? ? ? ? ? ? ? ? ? ? is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=0.5)
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? )
? ? ? ? )

? ? ? ? .set_global_opts(
? ? ? ? ? ? title_opts = opts.TitleOpts(title = flight_id[0]),
? ? ? ? ? ? yaxis_opts=opts.AxisOpts(name="運量"),
? ? ? ? ? ? datazoom_opts = opts.DataZoomOpts(),

? ? ? ? )
? ? )


? ? line = (
? ? ? ? Line()
? ? ? ? .add_xaxis(cargo_ab.flight_date.values.tolist())
? ? ? ? .add_yaxis("運價", cargo_ab.cargo_rate.values.tolist(),yaxis_index=1
? ? ? ? )
? ? )

? ? bar.overlap(line)
? ? return Grid().add(
? ? ? ? bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
? ? )


def cde_() -> Grid:
? ? bar = (
? ? ? ? Bar()
? ? ? ? .add_xaxis(cargo_cde.flight_date.values.tolist())
? ? ? ? .add_yaxis("運量", cargo_cde.cargo_weight.values.tolist(), yaxis_index=0)

? ? ? ? .set_series_opts(
? ? ? ? ? ? itemstyle_opts=opts.ItemStyleOpts(
? ? ? ? ? ? ? ? opacity=0.5,
? ? ? ? ? ? )
? ? ? ? ?)
? ? ? ? ?.extend_axis(
? ? ? ? ? ? yaxis=opts.AxisOpts(
? ? ? ? ? ? ? ? type_="value",
? ? ? ? ? ? ? ? name="運價",
? ? ? ? ? ? ? ? position="right",
? ? ? ? ? ? ? ? axisline_opts=opts.AxisLineOpts(
? ? ? ? ? ? ? ? ? ? linestyle_opts=opts.LineStyleOpts(color="#675bba")
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? splitline_opts=opts.SplitLineOpts(
? ? ? ? ? ? ? ? ? ? is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=0.5)
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? )
? ? ? ? )

? ? ? ? .set_global_opts(
? ? ? ? ? ? title_opts=opts.TitleOpts(title=flight_id[0]),
? ? ? ? ? ? yaxis_opts=opts.AxisOpts(name="運量"),
? ? ? ? ? ? datazoom_opts=opts.DataZoomOpts(),
? ? ? ? )
? ? )


? ? line = (
? ? ? ? Line()
? ? ? ? .add_xaxis(cargo_cde.flight_date.values.tolist())
? ? ? ? .add_yaxis("運價", cargo_cde.cargo_rate.values.tolist(),yaxis_index=1
? ? ? ? )
? ? )

? ? bar.overlap(line)
? ? return Grid().add(
? ? ? ? bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
? ? )

def m_() -> Grid:
? ? bar = (
? ? ? ? Bar()
? ? ? ? .add_xaxis(cargo_m.flight_date.values.tolist())
? ? ? ? .add_yaxis("運量", cargo_m.cargo_weight.values.tolist(), yaxis_index=0)

? ? ? ? .set_series_opts(
? ? ? ? ? ? itemstyle_opts=opts.ItemStyleOpts(
? ? ? ? ? ? ? ? opacity=0.5,
? ? ? ? ? ? )
? ? ? ? ?)
? ? ? ? ?.extend_axis(
? ? ? ? ? ? yaxis=opts.AxisOpts(
? ? ? ? ? ? ? ? type_="value",
? ? ? ? ? ? ? ? name="運價",
? ? ? ? ? ? ? ? position="right",
? ? ? ? ? ? ? ? axisline_opts=opts.AxisLineOpts(
? ? ? ? ? ? ? ? ? ? linestyle_opts=opts.LineStyleOpts(color="#675bba")
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? ? ? splitline_opts=opts.SplitLineOpts(
? ? ? ? ? ? ? ? ? ? is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=0.5)
? ? ? ? ? ? ? ? ),
? ? ? ? ? ? )
? ? ? ? )

? ? ? ? .set_global_opts(
? ? ? ? ? ? title_opts=opts.TitleOpts(title=flight_id[6]),
? ? ? ? ? ? yaxis_opts=opts.AxisOpts(name="運量"),
? ? ? ? ? ? datazoom_opts=opts.DataZoomOpts(),
? ? ? ? )
? ? )


? ? line = (
? ? ? ? Line()
? ? ? ? .add_xaxis(cargo_m.flight_date.values.tolist())
? ? ? ? .add_yaxis("運價", cargo_m.cargo_rate.values.tolist(),yaxis_index=1
? ? ? ? )
? ? )

? ? bar.overlap(line)
? ? return Grid().add(
? ? ? ? bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
? ? )


tab ?= Tab()
tab.add(ab_(), "A;B")
tab.add(cde_(), "C;D;E")
tab.add(m_(), "M")
tab.render_notebook()

下面結合效果圖對代碼做一下簡單的解析,整個代碼可以分3大塊,第一塊是連接數(shù)據(jù)庫讀取原數(shù)據(jù)并將數(shù)據(jù)一分為三,每一份數(shù)據(jù)為一個獨立的貨物類別;第二塊是各用一個函數(shù)實現(xiàn)某類別貨物cargo_weight和cargo_rate展示,而每一個函數(shù)作為Tab的參數(shù)進行調用,這樣,每一個類別形成一個Tab,每一個Tab下面剛好有這個id的歷史cargo_weight和cargo_rate走勢情況,這樣做樣做的好處,用Tab就可以劃分了cargo_type中"A;B", "C;D;E"和 "M"三個類別;最后調用render_notebook函數(shù)把所有Tab渲染出來。

結論:

效果圖可以看到如果只要畫一個id的各類貨物的cargo_weight和cargo_rate走勢的話,效果還是不錯的,然而我們的id數(shù)目高達400個,上述方法很難奏效,我們希望讓id去替換上圖的運量和運價兩個圖例,形成id簇的圖例,最好還可以對圖例進行選擇或者翻頁

原文鏈接:https://blog.csdn.net/zengbowengood/article/details/123151507

欄目分類
最近更新