5 分鐘完全解讀 pyecharts 動態(tài)圖表
點擊“簡說Python”,選擇“置頂/星標公眾號”
福利干貨,第一時間送達!
本文偏長(6k+字),實用性高,老表建議先收藏,然后轉(zhuǎn)發(fā)朋友圈,然后吃飯、休閑時慢慢看,反復看,反復記,反復練。
1. pyecharts簡介
pip install pyecharts
2.pyecharts簡單使用
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType
line = (
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width='1000px',height='300px' ))
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"),
datazoom_opts=opts.DataZoomOpts(is_show=True))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
)
line.render('test.html')
line.render_notebook()

圖標類型(Line)本身的初始化配置,如主題,大小 加載數(shù)據(jù):如加載x軸數(shù)據(jù),加載y軸數(shù)據(jù)(可以多個) 設置全局配置,如標題,區(qū)域縮放datazoom,工具箱等 設置系列配置項,如標簽,線條,刻度文本展示等 圖標顯示: render保存成html文件,如果是jupyter notebook則直接通過render_notebook展示在notebook中
3.常用配置使用

InitOpts:各個圖表類型初始配置 set_global_opts:全局外觀配置 set_series_opts:系列配置
InitOpts:主題,長寬,動畫效果 DataZoomOpts:區(qū)域收縮,這個對于數(shù)據(jù)特別多,如一天的時間序列數(shù)據(jù),特別有用,可以拖動查看全局和局部的數(shù)據(jù)(可以設置是否顯式顯式還是可拖動type_="inside") 標題配置TitleOpts:說明這個圖表說明的是什么,必備的吧 圖例配置LegendOpts:說明圖表中的不同數(shù)據(jù)項(這個圖例是可以點擊的,可以單獨查看某個圖例的數(shù)據(jù),很有用) 提示框配置TooltipOpts:顯示圖例具體某個點的數(shù)據(jù) x軸和y軸坐標軸標題說明AxisOpts 坐標刻度調(diào)整:特別適用于刻度說明比較多,可以顯示角度變換等 markpoint/markline: 對圖表的特別標記,用于重點說明部分和標注區(qū)分線
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,
width='1000px',
height='300px',
animation_opts=opts.AnimationOpts(animation_delay=1000, animation_easing="elasticOut")
)
)
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"),
toolbox_opts=opts.ToolboxOpts(is_show=False),
# datazoom_opts=opts.DataZoomOpts(is_show=True)
datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
legend_opts=opts.LegendOpts(type_="scroll", pos_left="50%", orient="vertical"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15), name="我是 X 軸"),
yaxis_opts=opts.AxisOpts(name="我是 Y 軸", axislabel_opts=opts.LabelOpts(formatter="{value} /月")),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
opts.MarkPointItem(type_="average", name="平均值"),
]
),
)
)
# line.render('test.html')
bar.render_notebook()

4.常用組合圖表使用
不同圖表類型組合如柱狀圖和折線圖組合在一張圖中(雙y軸),主要的看同一視角不同指標的差異和關聯(lián);pyecharts中是通過overlap實現(xiàn) from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.faker import Faker
v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
bar = (
Bar(init_opts=opts.InitOpts(width="680px", height="300px"))
.add_xaxis(Faker.months)
.add_yaxis("蒸發(fā)量", v1)
.add_yaxis("降水量", v2)
.extend_axis(
yaxis=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} °C"), interval=5
)
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Overlap-bar+line"),
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} ml")),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
)
)
line = Line().add_xaxis(Faker.months).add_yaxis("平均溫度", v3, yaxis_index=1)
bar.overlap(line)
bar.render_notebook()從實現(xiàn)上,
.extend_axis增加一個縱坐標增加的折線圖設置軸坐標時設置
yaxis_index索引和前面的縱坐標對應然后兩張疊加overlap
bar.overlap(line)

多圖標以網(wǎng)格(GRID)方式組合,主要是對比;pyecharts中是通過grid組件實現(xiàn) from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line
from pyecharts.faker import Faker
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar"))
)
line = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"),
legend_opts=opts.LegendOpts(pos_top="48%"),
)
)
grid = (
Grid(init_opts=opts.InitOpts(width="680px", height="500px"))
.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
.add(line, grid_opts=opts.GridOpts(pos_top="60%"))
)
grid.render_notebook()從實現(xiàn)看
主要通過
Grid把各種圖形放入其中各個圖表的位置通過
GridOpts來設置,上下左右的位置需要注意的是:grid中圖表的title和圖例需要根據(jù)所處位置來指定相對的位置(這個有點麻煩,多調(diào)調(diào))

5.地圖使用
Map類來實現(xiàn)的。具體細節(jié)需要注意:map支持不同的maptype,如中國地圖china(省級) china-cities(市級),世界地圖world,還有中國各省市地圖以及世界各國國家地圖,參看github pyecharts/datasets/map_filename.jsonmap的數(shù)據(jù)格式是(地理位置, value), 如 [['廣東', 76],['北京', 58]]可以通過 visualmap_opts查看著重點
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
c1 = (
Map()
.add("商家A", [list(z) for z in zip(Faker.guangdong_city, Faker.values())], "廣東")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-廣東地圖"), visualmap_opts=opts.VisualMapOpts()
)
)
c2 = (
Map()
.add("商家A", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-VisualMap(連續(xù)型)"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
# c1.render_notebook()
c2.render_notebook()

6.特色圖表使用
Timeline:時間線輪播多圖 先聲明一個 Timeline, 按照展示的時間順序,將圖表add到Timeline上; 可以通過播放按鈕循環(huán)按照時間順序展示圖表。from pyecharts import options as opts
from pyecharts.charts import Pie, Timeline
from pyecharts.faker import Faker
attr = Faker.choose()
tl = Timeline()
for i in range(2015, 2020):
pie = (
Pie()
.add(
"商家A",
[list(z) for z in zip(attr, Faker.values())],
rosetype="radius",
radius=["30%", "55%"],
)
.set_global_opts(title_opts=opts.TitleOpts("某商店{}年營業(yè)額".format(i)))
)
tl.add(pie, "{}年".format(i))
tl.render_notebook()

儀表盤
from pyecharts import options as opts
from pyecharts.charts import Gauge
c = (
Gauge()
.add("", [("完成率", 30.6)], radius="70%",
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(
color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30)
),
title_label_opts=opts.LabelOpts(
font_size=20, color="blue", font_family="Microsoft YaHei"
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"), legend_opts=opts.LegendOpts(is_show=False),)
)
c.render_notebook()

7.其他圖表一覽
雷達圖 Radar

樹形圖 Tree

熱力圖 heatMap

日歷圖 Calendar

散點圖 Scatter

3D圖 Bar3D

箱型圖 Boxplot

8.總結
pyecharts所有的圖像屬性設置都通過opts來設置,有圖表初始屬性/全局屬性/系列屬性 本文提供常用的配置,足夠用了,拿走不謝,見 常用配置使用pyecharts 支持多圖表組合,如折線圖和條形圖 overlap, 多個圖表grid展示 pyecharts好用的map,可以展示中國省市,世界各國地圖,請按照[位置,value]準備數(shù)據(jù) Timeline可以讓你的圖表按照時間輪播 更多圖表參見 參考資料
9.參考資料
https://pyecharts.org/#/zh-cn/quickstart
作者簡介:wedo實驗君, 數(shù)據(jù)分析師;熱愛生活,熱愛寫作
【圖書推薦】 《Python大數(shù)據(jù)分析從入門到精通》通過 3層技術架構+3套經(jīng)典數(shù)據(jù)+5個大數(shù)據(jù)平臺工具/引擎的Python庫+2個集成方向,教你輕松玩轉(zhuǎn)大數(shù)據(jù)分析。

京東4.23活動持續(xù)日期是4.21-4.24,絕大部分新技術的書都有5折優(yōu)惠哦~
【贈書規(guī)則】 本文留言說說你閱讀本文后的思考,留言字數(shù)需要超過15個字,我將選三位留言最走心的朋友,贈送圖書《Python大數(shù)據(jù)分析從入門到精通》一本。
活動截止時間:2021.4.27 20:00
注意:公眾號留言僅展示前100條留言;活動截止前一天內(nèi)的留言不入選;與留言主題無關留言或者留言字數(shù)不足15字的中獎無效,順延至相關留言;最終排名順序以我的截圖為準。
另外,為了讓更多讀者獲得贈書,本公眾號讀者一個月內(nèi)只能通過贈書活動獲得一次贈書;贈書閱讀后歡迎大家寫學習筆記投稿給本公眾號,每投稿兩次可以獲得贈書一本。
掃碼查看我朋友圈
獲取最新學習資源
學習更多: 整理了我開始分享學習筆記到現(xiàn)在超過250篇優(yōu)質(zhì)文章,涵蓋數(shù)據(jù)分析、爬蟲、機器學習等方面,別再說不知道該從哪開始,實戰(zhàn)哪里找了
“點贊”傳統(tǒng)美德不能丟 
評論
圖片
表情
