5 分鐘完全解讀 pyecharts 動態(tài)圖表
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="主標(biāo)題", subtitle="副標(biāo)題"),
datazoom_opts=opts.DataZoomOpts(is_show=True))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
)
line.render('test.html')
line.render_notebook()

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

InitOpts:各個圖表類型初始配置 set_global_opts:全局外觀配置 set_series_opts:系列配置
InitOpts:主題,長寬,動畫效果 DataZoomOpts:區(qū)域收縮,這個對于數(shù)據(jù)特別多,如一天的時間序列數(shù)據(jù),特別有用,可以拖動查看全局和局部的數(shù)據(jù)(可以設(shè)置是否顯式顯式還是可拖動type_="inside") 標(biāo)題配置TitleOpts:說明這個圖表說明的是什么,必備的吧 圖例配置LegendOpts:說明圖表中的不同數(shù)據(jù)項(xiàng)(這個圖例是可以點(diǎn)擊的,可以單獨(dú)查看某個圖例的數(shù)據(jù),很有用) 提示框配置TooltipOpts:顯示圖例具體某個點(diǎn)的數(shù)據(jù) x軸和y軸坐標(biāo)軸標(biāo)題說明AxisOpts 坐標(biāo)刻度調(diào)整:特別適用于刻度說明比較多,可以顯示角度變換等 markpoint/markline: 對圖表的特別標(biāo)記,用于重點(diǎn)說明部分和標(biāo)注區(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="主標(biāo)題", subtitle="副標(biāo)題"),
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軸),主要的看同一視角不同指標(biāo)的差異和關(guān)聯(lián);pyecharts中是通過overlap實(shí)現(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()從實(shí)現(xiàn)上,
.extend_axis增加一個縱坐標(biāo)增加的折線圖設(shè)置軸坐標(biāo)時設(shè)置
yaxis_index索引和前面的縱坐標(biāo)對應(yīng)然后兩張疊加overlap
bar.overlap(line)

多圖標(biāo)以網(wǎng)格(GRID)方式組合,主要是對比;pyecharts中是通過grid組件實(shí)現(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()從實(shí)現(xiàn)看
主要通過
Grid把各種圖形放入其中各個圖表的位置通過
GridOpts來設(shè)置,上下左右的位置需要注意的是:grid中圖表的title和圖例需要根據(jù)所處位置來指定相對的位置(這個有點(diǎn)麻煩,多調(diào)調(diào))

5.地圖使用
Map類來實(shí)現(xiàn)的。具體細(xì)節(jié)需要注意:map支持不同的maptype,如中國地圖china(省級) china-cities(市級),世界地圖world,還有中國各省市地圖以及世界各國國家地圖,參看github pyecharts/datasets/map_filename.jsonmap的數(shù)據(jù)格式是(地理位置, value), 如 [['廣東', 76],['北京', 58]]可以通過 visualmap_opts查看著重點(diǎn)
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("某商店{}年?duì)I業(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.其他圖表一覽
雷達(dá)圖 Radar

樹形圖 Tree

熱力圖 heatMap

日歷圖 Calendar

散點(diǎn)圖 Scatter

3D圖 Bar3D

箱型圖 Boxplot

8.總結(jié)
pyecharts所有的圖像屬性設(shè)置都通過opts來設(shè)置,有圖表初始屬性/全局屬性/系列屬性 本文提供常用的配置,足夠用了,拿走不謝,見 常用配置使用pyecharts 支持多圖表組合,如折線圖和條形圖 overlap, 多個圖表grid展示 pyecharts好用的map,可以展示中國省市,世界各國地圖,請按照[位置,value]準(zhǔn)備數(shù)據(jù) Timeline可以讓你的圖表按照時間輪播 更多圖表參見 參考資料
9.參考資料
https://pyecharts.org/#/zh-cn/quickstart
作者簡介:wedo實(shí)驗(yàn)君, 數(shù)據(jù)分析師;熱愛生活,熱愛寫作
贊 賞 作 者

更多閱讀
特別推薦

點(diǎn)擊下方閱讀原文加入社區(qū)會員
評論
圖片
表情
