PyEcharts应⽤教程及实例
ECharts是⼀款基于JavaScript的数据可视化图表库,提供直观,⽣动,可交互,可个性化定制的数据可视化图表。ECharts最初由百度团队开源,并于2018年初捐赠给Apache基⾦会,成为ASF孵化级项⽬。
ECharts官⽹:
无补于事>做梦梦见捡钱PyEcharts 是⼀个⽤于⽣成 Echarts 图表的类库。 Python 是⼀门富有表达⼒的语⾔,很适合⽤于数据处理。当数据分析遇上数据可视化时,PyEcharts 诞⽣了。
⽬录
⼀、安装
中山大学招生简章
pip install pyecharts
⼆、操作
from pyecharts import options # 图形样式编辑
from pyecharts.charts import Graph/Bar/Line/... # 引⼊不同图(关系图、柱状图、折线图...)
1、折线图(柱状图、散点图适⽤)
from pyecharts import options
from pyecharts.charts import Line/Bar/Scatter
def draw(xlist, ylist):
line = Line()
line.add_xaxis(xlist) # x坐标名称
line.add_yaxis("线名称", ylist, is_smooth=True) # 线名称、各点数值、是否平滑
line.t_global_opts(title_opts=options.TitleOpts(title='name')) # 表名称
def main():
xlist = ['⼀', '⼆', '三', '四', '五']
ylist = [3, 9, 15, 21, 27]
draw(xlist, ylist)
if __name__ == '__main__':
main()
2、饼状图、环状图
from pyecharts import options as opts
from pyecharts.charts import Pie
def draw(blist, nlist):
pie = Pie()
# 各部分名称,值;添加radius(内环⼤⼩、外环⼤⼩)即为环状
pie.add("", [list(i) for i in zip(nlist, blist)], radius=["40%", "75%"])
# 表名称
pie.t_global_opts(title_opts=opts.TitleOpts(title="name"))
# 显⽰名称及值
pie.t_ries_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) # ⽣成html⽂件
def main():
nlist = ['⼀', '⼆', '三', '四', '五']
blist = [3, 9, 15, 21, 27]
draw(blist, nlist)
if __name__ == '__main__':
main()
3、关系图
from pyecharts import options as opts
from pyecharts.charts import Graph
def draw(alist, value):
nodes = []
for i in range(5):
nodes.append({'name': alist[i], 'value': value[i]}) # 设置节点名称及对应值(字典格式)
links = []
for i in nodes[2:]:
links.append(opts.GraphLink(source='A', ('name')))
屏声敛息for i in nodes[2:]:
links.append(opts.GraphLink(source='B', ('name'))) # 连线,指定连线的起始和⽬标节点
graph = Graph()
graph.add("", nodes, links, edge_symbol=['circle', 'arrow'], repulsion=200, is_draggable=True, layout='force') # repulsion节点间排斥⼒,is_draggable是否可拖拽 graph.t_global_opts(title_opts=opts.TitleOpts(title="name")) # 表名称
什么致勃勃成语>食用增稠剂def main():
value = [0, 0, 5, 32, 45]
alist = ['A', 'B', 'c', 'd', 'e']
draw(alist, value)
if __name__ == '__main__':
main()
4、词云
from pyecharts.charts import WordCloud
data = [('天⽓', 23), ('美丽', 10), ('天空', 5) ,('飞鸟', 15) , ('如诗如画', 25) , ('⼼情', 5), ('哈哈', 17)]
#⾯向对象⽅式
cloudObj = WordCloud()
# shap默认为cicrle,其他形状有circle, cardioid, diamond, triangle-forward, triangle, pentagon, star cl
oudObj.add('', data, shape='circle')
# shap可更换为图⽚,但尝试后效果不好。图⽚存在bug,需刷新⽹页后才会显⽰。
# cloudObj.add('', data, mask_image='C:\XXX.png')
5、地图
(1)基础地图
from pyecharts.charts import Map
from pyecharts import options as opts
def map(provinces, value):
map = Map()
# china可改为world或各省市名称
ant怎么读map.add("", [list(i) for i in zip(provinces, value)], "china")
# 表名称;max_为最⼤值,超过此值都为最⼤值颜⾊;split_number,is_piecewi可以将颜⾊分块显⽰
map.t_global_opts(title_opts=opts.TitleOpts(title="title"), visualmap_opts=opts.VisualMapOpts(max_=5000, split_number=8, is_piecewi=True)) der('xxx.html')
def main():
provinces = ["⼴东", "北京", "上海", "辽宁", "湖南", "四川", "西藏"]
我的身体value = [300, 100, 2000, 800, 10000, 400, 5000]
map(provinces, value)
if __name__ == '__main__':
main()
(2)热⼒地图