ECharts開源來自百度商業(yè)前端數(shù)據(jù)可視化團(tuán)隊(duì)憎瘸,基于html5 Canvas入篮,是一個(gè)純Javascript圖表庫,提供直觀幌甘,生動(dòng)潮售,可交互,可個(gè)性化定制的數(shù)據(jù)可視化圖表锅风。
pyecharts包含的圖表
Bar(柱狀圖/條形圖)
Bar3D(3D 柱狀圖)
Boxplot(箱形圖)
EffectScatter(帶有漣漪特效動(dòng)畫的散點(diǎn)圖)
Funnel(漏斗圖)
Gauge(儀表盤)
Geo(地理坐標(biāo)系)
Graph(關(guān)系圖)
HeatMap(熱力圖)
Kline(K線圖)
Line(折線/面積圖)
Line3D(3D 折線圖)
Liquid(水球圖)
Map(地圖)
Parallel(平行坐標(biāo)系)
Pie(餅圖)
Polar(極坐標(biāo)系)
Radar(雷達(dá)圖)
Sankey(伤址蹋基圖)
Scatter(散點(diǎn)圖)
Scatter3D(3D 散點(diǎn)圖)
ThemeRiver(主題河流圖)
WordCloud(詞云圖)
用戶自定義
Grid 類:并行顯示多張圖
Overlap 類:結(jié)合不同類型圖表疊加畫在同張圖上
Page 類:同一網(wǎng)頁按順序展示多圖
Timeline 類:提供時(shí)間線輪播多張圖
pyecharts安裝
pip install pyecharts
自從 0.3.2 開始,為了縮減項(xiàng)目本身的體積以及維持 pyecharts 項(xiàng)目的輕量化運(yùn)行皱埠,pyecharts 將不再自帶地圖 js 文件肮帐。如用戶需要用到地圖圖表(Geo、Map)边器,可自行安裝對應(yīng)的地圖文件包训枢。
- 全球國家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個(gè)國家,包括中國地圖
- 中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個(gè)省忘巧,5 個(gè)自治區(qū)
- 中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 個(gè)中國城市
Windows下通過已下的pip命令進(jìn)行安裝
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
Linux下通過以下的pip命令進(jìn)行安裝
sudo pip3 install echarts-countries-pypkg
sudo pip3 install echarts-china-provinces-pypkg
sudo pip3 install echarts-china-cities-pypkg
sudo pip3 install echarts-countries-pypkg
sudo pip3 install echarts-china-provinces-pypkg
sudo pip3 install echarts-china-cities-pypkg
bar = Bar("我的第一個(gè)圖表","這里是副標(biāo)題")
bar.add("服裝",["帽子","口罩","羊毛衫","褲子","高跟鞋","襪子"],[44,34,56,23,34,22]) #如果兩個(gè)列表是一個(gè)字典或元祖恒界,使用cast可以變成兩個(gè)列表
bar.render()
在add中加一個(gè):is_more_utils=True,可以查看更多配置選項(xiàng)。
柱狀圖示例
from pyecharts import Bar # 柱狀圖
attr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
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]
#創(chuàng)建柱狀圖
bar = Bar("Bar chart", "precipitation and evaporation one year")
#添加數(shù)據(jù)
bar.add("precipitation", attr, v1, mark_line=["average"], mark_point=["max", "min"])
bar.add("evaporation", attr, v2, mark_line=["average"], mark_point=["max", "min"])
# 生成一個(gè)html文件
bar.render()
Tip:可以按右邊的下載按鈕將圖片下載到本地
add()
主要方法砚嘴,用于添加圖表的數(shù)據(jù)和設(shè)置各種配置項(xiàng)
show_config()
打印輸出圖表的所有配置項(xiàng)
render()
默認(rèn)將會(huì)在根目錄下生成一個(gè) render.html 的文件十酣,支持 path 參數(shù)涩拙,設(shè)置文件保存位置,如 render(r"e:my_first_chart.html")耸采,文件用瀏覽器打開兴泥。
默認(rèn)的編碼類型為 UTF-8,在 Python3 中是沒什么問題的虾宇,Python3 對中文的支持好很多搓彻。但是在 Python2 中,編碼的處理是個(gè)很頭疼的問題嘱朽,暫時(shí)沒能找到完美的解決方法好唯,目前只能通過文本編輯器自己進(jìn)行二次編碼,我用的是 Visual Studio Code燥翅,先通過 Gbk 編碼重新打開,然后再用 UTF-8 重新保存蜕提,這樣用瀏覽器打開的話就不會(huì)出現(xiàn)中文亂碼問題了森书。
雷達(dá)圖示例
from pyecharts import Radar
schema =[ ("銷售", 6500), ("管理", 16000), ("信息技術(shù)", 30000), ("客服", 38000), ("研發(fā)", 52000), ("市場", 25000)]
v1 =[[4300, 10000, 28000, 35000, 50000, 19000]]
v2 =[[5000, 14000, 28000, 31000, 42000, 21000]]
radar =Radar()
radar.config(schema)
radar.add("預(yù)算分配", v1, is_splitline=True, is_axisline_show=True)
radar.add("實(shí)際開銷", v2, label_color=["#4e79a7"], is_area_show=False)
radar.show_config()
radar.render()
散點(diǎn)圖示例
from pyecharts import Scatter
v1 =[10, 20, 30, 40, 50, 60]
v2 =[10, 20, 30, 40, 50, 60]
scatter =Scatter("散點(diǎn)圖示例")
scatter.add("A", v1, v2)
scatter.add("B", v1[::-1], v2)
scatter.show_config()
scatter.render()
散點(diǎn)打印Pyecharts字體。
from pyecharts import Scatter
scatter =Scatter("散點(diǎn)圖示例")
v1,v2=scatter.draw(r".\wordcloud.png")
scatter.add("pyecharts", v1, v2, is_random=True)
scatter.show_config()
scatter.render()
帶有漣漪特效動(dòng)畫的散點(diǎn)圖
from pyecharts import EffectScatter
v1 =[10, 20, 30, 40, 50, 60]
v2 =[25, 20, 15, 10, 60, 33]
es =EffectScatter("動(dòng)態(tài)散點(diǎn)圖示例")
es.add("effectScatter", v1, v2)
es.render()
es =EffectScatter("動(dòng)態(tài)散點(diǎn)圖各種圖形示例")
es.add("", [10], [10], symbol_size=20, effect_scale=3.5, effect_period=3, symbol="pin")
es.add("", [20], [20], symbol_size=12, effect_scale=4.5, effect_period=4,symbol="rect")
es.add("", [30], [30], symbol_size=30, effect_scale=5.5, effect_period=5,symbol="roundRect")
es.add("", [40], [40], symbol_size=10, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
es.add("", [50], [50], symbol_size=16, effect_scale=5.5, effect_period=3,symbol="arrow")
es.add("", [60], [60], symbol_size=6, effect_scale=2.5, effect_period=3,symbol="triangle")
es.render()
map
from pyecharts import Map
value =[155, 10, 66, 78, 33, 80, 190, 53, 49.6]
attr =["福建", "山東", "北京", "上海", "甘肅", "新疆", "河南", "廣西", "西藏"]
map=Map("Map 結(jié)合 VisualMap 示例", width=1200, height=600)
map.add("", attr, value, maptype='china', is_visualmap=True, visual_text_color='#000')
map.show_config()
map.render()
各類電影中"好片"所占的比例
from pyecharts import Pie
pie =Pie('各類電影中"好片"所占的比例', "數(shù)據(jù)來著豆瓣", title_pos='center')
pie.add("", ["劇情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, )
pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left')
pie.add("", ["愛情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["驚悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["冒險(xiǎn)", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["動(dòng)作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["喜劇", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["懸疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center")
pie.show_config()
pie.render(path=r'.\render.html')
from pyecharts import Line, Bar, Pie, EffectScatter
# 數(shù)據(jù)
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]
line = Line('折線圖')
line.add('商家A', attr, v1, mark_point=['max'])
line.add('商家B', attr, v2, mark_point=['min'], is_smooth=True)
line.show_config()
line.render(path=r'.\render.html')
詞云圖
from pyecharts import WordCloud
name =['Sam S Club', 'Macys', 'Amy Schumer', 'Jurassic World', 'Charter Communications', 'Chick Fil A', 'Planet Fitness', 'Pitch Perfect', 'Express', 'Home', 'Johnny Depp', 'Lena Dunham', 'Lewis Hamilton', 'KXAN', 'Mary Ellen Mark', 'Farrah Abraham', 'Rita Ora', 'Serena Williams', 'NCAA baseball tournament', 'Point Break']
value =[10000, 6181, 4386, 4055, 2467, 2244, 1898, 1484, 1112, 965, 847, 582, 555, 550, 462, 366, 360, 282, 273, 265]
wordcloud =WordCloud(width=1300, height=620)
wordcloud.add("", name, value, word_size_range=[20, 100])
wordcloud.show_config()
wordcloud.render()
詞云圖的形狀可以通過shape參數(shù)來選擇
wordcloud.add("", name, value, word_size_range=[20, 100],shape='star')'
餅圖和玫瑰餅圖
from pyecharts import Line, Bar, Pie, EffectScatter
attr =["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]
pie = Pie('餅圖')
pie.add('芝麻餅', attr, v1, is_label_show=True)
pie.show_config()
pie.render(path=r'.\render.html')
pie2 = Pie("餅圖-玫瑰圖示例", title_pos='center', width=900)
pie2.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
pie2.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True)
pie2.show_config()
pie2.render(path=r'.\render.html')
關(guān)系圖
from pyecharts import Graph
nodes = [{"name": "結(jié)點(diǎn)1", "symbolSize": 10},{"name": "結(jié)點(diǎn)2", "symbolSize": 20},{"name": "結(jié)點(diǎn)3", "symbolSize": 30},{"name": "結(jié)點(diǎn)4", "symbolSize": 40},{"name": "結(jié)點(diǎn)5", "symbolSize": 50},{"name": "結(jié)點(diǎn)6", "symbolSize": 40},{"name": "結(jié)點(diǎn)7", "symbolSize": 30},{"name": "結(jié)點(diǎn)8", "symbolSize": 20}]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get('name'), "target": j.get('name')})
graph = Graph("關(guān)系圖-環(huán)形布局示例")
graph.add("關(guān)系圖1",nodes,links,is_label_show=True,repulsion=8000,layout='circular',label_text_color=None)
graph.show_config()
graph.render()