一、PyEcharts簡介
??“pyecharts 是一個用于生成 Echarts 圖表的類庫季俩。Echarts 是百度開源的一個數據可視化 JS 庫。用 Echarts 生成的圖可視化效果非常棒榆俺,為了與 Python 進行對接历葛,方便在 Python 中直接使用數據生成圖”。
??pyecharts可以展示動態(tài)圖当纱,在線報告使用比較美觀呛每,并且展示數據方便,鼠標懸停在圖上坡氯,即可顯示數值晨横、標簽等洋腮。
官網地址:http://pyecharts.org/#/zh-cn/charts
https://pyecharts.org/#/zh-cn/quickstart
https://github.com/pyecharts/pyecharts
二、模塊安裝
下載安裝:(推薦)
https://pypi.org/project/pyecharts/0.1.9.4/#files
pip install pyecharts-0.1.9.4-py2.py3-none-any.whl
在線安裝手形,不成功:
(film) C:\Users\Administrator>pip install pyecharts
三啥供、圖表示例
- 柱形圖
# -*- coding: utf-8 -*-
from pyecharts import Bar
'''
柱形圖
'''
bar = Bar("我的第一個圖表", "這里是副標題")
bar.add("服裝", ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], [5, 20, 36, 10, 75, 90])
bar.show_config()
bar.render()
會生成Html文件(render.html):
- 柱狀圖數據堆疊
# -*- coding: utf-8 -*-
from pyecharts import Bar
'''
柱狀圖數據堆疊
'''
attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar("柱狀圖數據堆疊示例")
bar.add("商家A", attr, v1, is_stack=True)
bar.add("商家B", attr, v2, is_stack=True)
bar.show_config()
bar.render()
- 散點圖
# -*- coding: utf-8 -*-
from pyecharts import EffectScatter
'''
散點圖
'''
v1 = [10, 20, 30, 40, 50, 60]
v2 = [25, 20, 15, 10, 60, 33]
es = EffectScatter("帶有漣漪特效動畫的動態(tài)散點圖示例")
es.add("effectScatter", v1, v2)
es.render()
- 漏斗圖
# -*- coding: utf-8 -*-
from pyecharts import Funnel
'''
漏斗圖
'''
attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
value = [20, 40, 60, 80, 100, 120]
funnel = Funnel("漏斗圖示例")
funnel.add("商品", attr, value, is_label_show=True, label_pos="inside", label_text_color="#fff")
funnel.render()
- 餅圖
# -*- coding: utf-8 -*-
from pyecharts import Pie
'''
餅圖
'''
attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("餅圖示例")
pie.add("", attr, v1, is_label_show=True)
pie.render()
- 圓環(huán)圖
# -*- coding: utf-8 -*-
from pyecharts import Pie
'''
圓環(huán)圖
'''
attr = ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
v1 = [11, 12, 13, 10, 10, 10]
pie = Pie("餅圖-圓環(huán)圖示例", title_pos='center')
pie.add("", attr, v1, radius=[40, 75], label_text_color=None,
is_label_show=True, legend_orient='vertical',
legend_pos='left')
pie.render()
- 儀表盤
# -*- coding: utf-8 -*-
from pyecharts import Gauge
'''
儀表盤
'''
gauge = Gauge("儀表盤示例")
gauge.add("業(yè)務指標", "完成率", 66.66)
gauge.show_config()
gauge.render()
- 地理坐標系
需安裝模塊:
$ pip install echarts-countries-pypkg
$ pip install echarts-china-provinces-pypkg
$ pip install echarts-china-cities-pypkg
$ pip install echarts-china-counties-pypkg
$ pip install echarts-china-misc-pypkg
$ pip install echarts-united-kingdom-pypkg
- 全球國家地圖: echarts-countries-pypkg (1.9MB):世界地圖和 213 個國家,包括中國地圖库糠。
- 中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個省伙狐,5 個自治區(qū)。
- 中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 個中國城市曼玩。
中國地圖在 echarts-countries-pypkg
1)中國地圖
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
中國地圖
'''
# 省和直轄市
province_distribution = {'河南': 45.23, '北京': 37.56, '河北': 21, '遼寧': 12, '江西': 6, '上海': 20, '安徽': 10, '江蘇': 16, '湖南': 9, '浙江': 13, '海南': 2, '廣東': 22, '湖北': 8, '黑龍江': 11, '澳門': 1, '陜西': 11, '四川': 7, '內蒙古': 3, '重慶': 3, '云南': 6, '貴州': 2, '吉林': 3, '山西': 12, '山東': 11, '福建': 4, '青海': 1, '舵主科技鳞骤,質量保證': 1, '天津': 1, '其他': 1}
provice=list(province_distribution.keys())
values=list(province_distribution.values())
# maptype='china' 只顯示全國直轄市和省級
# 數據只能是省名和直轄市的名稱
map = Map("中國地圖",'中國地圖', width=1200, height=600)
map.add("", provice, values, visual_range=[0, 50], maptype='china', is_visualmap=True,
visual_text_color='#000')
map.show_config()
map.render(path="./data/04-01中國地圖.html")
2)省份地圖
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
中國地圖
'''
# 城市 -- 指定省的城市 xx市
city = ['鄭州市', '安陽市', '洛陽市', '濮陽市', '南陽市', '開封市', '商丘市', '信陽市', '新鄉(xiāng)市']
values2 = [1.07, 3.85, 6.38, 8.21, 2.53, 4.37, 9.38, 4.29, 6.1]
# 河南地圖 數據必須是省內放入城市名
map2 = Map("河南地圖",'河南', width=1200, height=600)
map2.add('河南', city, values2, visual_range=[1, 10], maptype='河南', is_visualmap=True, visual_text_color='#000')
map2.show_config()
map2.render(path="./data/04-02河南地圖.html")
3)區(qū)縣地圖--不顯示
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
區(qū)縣地圖
'''
# 區(qū)縣 -- 具體城市內的區(qū)縣 xx縣
quxian = ['夏邑縣', '民權縣', '梁園區(qū)', '睢陽區(qū)', '柘城縣', '寧陵縣']
values3 = [3, 5, 7, 8, 2, 4]
# # 商丘地圖 數據為商丘市下的區(qū)縣
map3 = Map("商丘地圖",'商丘', width=1200, height=600)
map3.add("商丘", quxian, values3, visual_range=[1, 10], maptype='商丘', is_visualmap=True,
visual_text_color='#000')
map3.show_config()
map3.render(path="./data/04-03商丘地圖.html")
4)熱力分布圖
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
熱力圖
'''
data = [
("海門", 9),("鄂爾多斯", 12),("招遠", 12),("舟山", 12),("齊齊哈爾", 14),("鹽城", 15),
("赤峰", 16),("青島", 18),("乳山", 18),("金昌", 19),("泉州", 21),("萊西", 21),
("日照", 21),("膠南", 22),("南通", 23),("拉薩", 24),("云浮", 24),("梅州", 25)]
geo = Geo("全國主要城市空氣質量熱力圖", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("空氣質量熱力圖", attr, value, visual_range=[0, 25], type='heatmap',visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False)
geo.show_config()
geo.render(path="./data/04-04空氣質量熱力圖.html")
5)空氣質量評分
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
熱力圖
'''
# 空氣質量評分
indexs = ['上海', '北京', '合肥', '哈爾濱', '廣州', '成都', '無錫', '杭州', '武漢', '深圳', '西安', '鄭州', '重慶', '長沙']
values = [4.07, 1.85, 4.38, 2.21, 3.53, 4.37, 1.38, 4.29, 4.1, 1.31, 3.92, 4.47, 2.40, 3.60]
geo = Geo("全國主要城市空氣質量評分", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59')
# type="effectScatter", is_random=True, effect_scale=5 使點具有發(fā)散性
geo.add("空氣質量評分", indexs, values, type="effectScatter", is_random=True, effect_scale=5, visual_range=[0, 5],visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False)
geo.show_config()
geo.render(path="./data/04-05空氣質量評分.html")
6)世界地圖
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
'''
世界地圖
'''
# 世界地圖數據
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr= ["China", "Canada", "Brazil", "Russia", "United States"]
map0 = Map("世界地圖示例", width=1200, height=600)
map0.add("世界地圖", attr, value, maptype="world", is_visualmap=True, visual_text_color='#000')
map0.render(path="./data/04-00世界地圖.html")
- 水球圖
# -*- coding: utf-8 -*-
from pyecharts import Liquid
'''
水球圖
'''
liquid =Liquid("水球圖")
liquid.add("Liquid", [0.6])
liquid.show_config()
liquid.render(path='./data/03-01水球.html')
# 圓形水球
liquid2 =Liquid("水球圖示例")
liquid2.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_outline_show=False)
liquid2.show_config()
liquid2.render(path='./data/03-02圓形水球.html')
# 菱形水球
liquid3 =Liquid("水球圖示例")
liquid3.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_animation=False, shape='diamond')
liquid3.show_config()
liquid3.render(path='./data/03-03菱形水球.html')
- 極坐標圖
# -*- coding: utf-8 -*-
from pyecharts import Polar
'''
極坐標圖
'''
# 極坐標
radius =['周一', '周二', '周三', '周四', '周五', '周六', '周日']
polar =Polar("極坐標系-堆疊柱狀圖示例", width=1200, height=600)
polar.add("A", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("B", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("C", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barRadius', is_stack=True)
polar.show_config()
polar.render(path='./data/03-04極坐標.html')
- 雷達圖
# -*- coding: utf-8 -*-
from pyecharts import Radar
'''
雷達圖
'''
# 雷達圖
schema =[ ("銷售", 6500), ("管理", 16000), ("信息技術", 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("預算分配", v1, is_splitline=True, is_axisline_show=True)
radar.add("實際開銷", v2, label_color=["#4e79a7"], is_area_show=False)
radar.show_config()
radar.render(path='./data/03-05雷達圖.html')
四、不同格式存儲
支持保存做種格式
如果想直接將圖片保存為 png, pdf, gif 格式的文件黍判,可以使用 pyecharts-snapshot。使用該插件請確保你的系統上已經安裝了 Nodejs 環(huán)境篙梢。
1.安裝phantomjs: conda install phantomjs
2.安裝 pyecharts-snapshot : pip install pyecharts-snapshot
2.調用 render 方法 bar.render(path='snapshot.png') 文件結尾可以為 svg/jpeg/png/pdf/gif顷帖。請注意,svg 文件需要你在初始化 bar 的時候設置 renderer='svg'渤滞。
對象.render(path='snapshot.html')
對象.render(path='snapshot.png')
對象.render(path='snapshot.pdf')