当前位置:   article > 正文

PyEcharts应用教程及实例_pyecharts.charts

pyecharts.charts

ECharts是一款基于JavaScript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。ECharts最初由百度团队开源,并于2018年初捐赠给Apache基金会,成为ASF孵化级项目。

ECharts官网:Apache ECharts

PyEcharts 是一个用于生成 Echarts 图表的类库。 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,PyEcharts 诞生了。

目录

一、安装

二、操作

1、折线图(柱状图、散点图适用)

2、饼状图、环状图

3、关系图

4、词云

5、地图

6、雷达图

7、树形图

8、 主题河流图

9、 折柱混合图

10、 工具(补充)


一、安装

pip install pyecharts

二、操作

  1. from pyecharts import options # 图形样式编辑
  2. from pyecharts.charts import Graph/Bar/Line/... # 引入不同图(关系图、柱状图、折线图...)

1、折线图(柱状图、散点图适用)

  1. from pyecharts import options
  2. from pyecharts.charts import Line/Bar/Scatter
  3. def draw(xlist, ylist):
  4. line = Line()
  5. line.add_xaxis(xlist) # x坐标名称
  6. line.add_yaxis("线名称", ylist, is_smooth=True) # 线名称、各点数值、是否平滑
  7. line.set_global_opts(title_opts=options.TitleOpts(title='name')) # 表名称
  8. line.render('xxx.html') # 生成html文件
  9. def main():
  10. xlist = ['一', '二', '三', '四', '五']
  11. ylist = [3, 9, 15, 21, 27]
  12. draw(xlist, ylist)
  13. if __name__ == '__main__':
  14. main()

2、饼状图、环状图

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Pie
  3. def draw(blist, nlist):
  4. pie = Pie()
  5. # 各部分名称,值;添加radius(内环大小、外环大小)即为环状
  6. pie.add("", [list(i) for i in zip(nlist, blist)], radius=["40%", "75%"])
  7. # 表名称
  8. pie.set_global_opts(title_opts=opts.TitleOpts(title="name"))
  9. # 显示名称及值
  10. pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
  11. # 生成html文件
  12. pie.render('xxx.html')
  13. def main():
  14. nlist = ['一', '二', '三', '四', '五']
  15. blist = [3, 9, 15, 21, 27]
  16. draw(blist, nlist)
  17. if __name__ == '__main__':
  18. main()

3、关系图

  1. from pyecharts import options as opts
  2. from pyecharts.charts import Graph
  3. def draw(alist, value):
  4. nodes = []
  5. for i in range(5):
  6. nodes.append({'name': alist[i], 'value': value[i]}) # 设置节点名称及对应值(字典格式)
  7. links = []
  8. for i in nodes[2:]:
  9. links.append(opts.GraphLink(source='A', target=i.get('name')))
  10. for i in nodes[2:]:
  11. links.append(opts.GraphLink(source='B', target=i.get('name'))) # 连线,指定连线的起始和目标节点
  12. graph = Graph()
  13. graph.add("", nodes, links, edge_symbol=['circle', 'arrow'], repulsion=200, is_draggable=True, layout='force') # repulsion节点间排斥力,is_draggable是否可拖拽节点,layout布局方式(有circular环形布局,force力引导布局,none无)
  14. graph.set_global_opts(title_opts=opts.TitleOpts(title="name")) # 表名称
  15. graph.render('xxx.html')
  16. def main():
  17. value = [0, 0, 5, 32, 45]
  18. alist = ['A', 'B', 'c', 'd', 'e']
  19. draw(alist, value)
  20. if __name__ == '__main__':
  21. main()

搜狗截图20210928172618.png

4、词云

  1. from pyecharts.charts import WordCloud
  2. data = [('天气', 23), ('美丽', 10), ('天空', 5) ,('飞鸟', 15) , ('如诗如画', 25) , ('心情', 5), ('哈哈', 17)]
  3. #面向对象方式
  4. cloudObj = WordCloud()
  5. # shap默认为cicrle,其他形状有circle, cardioid, diamond, triangle-forward, triangle, pentagon, star
  6. cloudObj.add('', data, shape='circle')
  7. cloudObj.render('xxx.html')
  8. # shap可更换为图片,但尝试后效果不好。图片存在bug,需刷新网页后才会显示。
  9. # cloudObj.add('', data, mask_image='C:\XXX.png')

图片.png

5、地图

(1)基础地图

  1. from pyecharts.charts import Map
  2. from pyecharts import options as opts
  3. def map(provinces, value):
  4. map = Map()
  5. # china可改为world或各省市名称
  6. map.add("", [list(i) for i in zip(provinces, value)], "china")
  7. # 表名称;max_为最大值,超过此值都为最大值颜色;split_number,is_piecewise可以将颜色分块显示
  8. map.set_global_opts(title_opts=opts.TitleOpts(title="title"), visualmap_opts=opts.VisualMapOpts(max_=5000, split_number=8, is_piecewise=True))
  9. map.render('xxx.html')
  10. def main():
  11. provinces = ["广东", "北京", "上海", "辽宁", "湖南", "四川", "西藏"]
  12. value = [300, 100, 2000, 800, 10000, 400, 5000]
  13. map(provinces, value)
  14. if __name__ == '__main__':
  15. main()

image.png

 (2)热力地图

  1. from pyecharts.charts import Geo
  2. from pyecharts import options as opts
  3. from pyecharts.globals import ChartType
  4. def map(provinces, value):
  5. map = Geo()
  6. # 仅以下两行与基础地图不同
  7. map.add_schema(maptype="china")
  8. map.add("", [list(i) for i in zip(provinces, value)], type_=ChartType.HEATMAP)
  9. map.set_global_opts(title_opts=opts.TitleOpts(title="title"),
  10. visualmap_opts=opts.VisualMapOpts(max_=5000))
  11. map.render('xxx.html')
  12. def main():
  13. provinces = ["广东", "北京", "上海", "辽宁", "湖南", "四川", "西藏"]
  14. value = [300, 100, 2000, 800, 10000, 400, 5000]
  15. map(provinces, value)
  16. if __name__ == '__main__':
  17. main()

image.png

6、雷达图

  1. from pyecharts.charts import Radar
  2. from pyecharts import options as opts
  3. def radar(v1):
  4. radar = Radar(init_opts=opts.InitOpts())
  5. radar.add_schema(
  6. schema=[
  7. opts.RadarIndicatorItem(name="传球", max_=100),
  8. opts.RadarIndicatorItem(name="射门", max_=100),
  9. opts.RadarIndicatorItem(name="身体", max_=100),
  10. opts.RadarIndicatorItem(name="防守", max_=100),
  11. opts.RadarIndicatorItem(name="速度", max_=100),
  12. opts.RadarIndicatorItem(name="盘带", max_=100),
  13. ],
  14. splitarea_opt=opts.SplitAreaOpts(is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), # 是否显示分隔区域,透明度为1
  15. textstyle_opts=opts.TextStyleOpts(color="#000000")
  16. )
  17. radar.add(series_name="C·罗纳尔多", data=v1, areastyle_opts=opts.AreaStyleOpts(color="#FF0000", opacity=0.2)) # 区域面积,透明度
  18. radar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
  19. radar.set_global_opts(title_opts=opts.TitleOpts(title="标准球员属性雷达图"), legend_opts=opts.LegendOpts(selected_mode="single")) # 可以设置单一选择查看
  20. radar.render('xxx.html')
  21. def main():
  22. v1 = [[83, 92, 87, 49, 89, 86]]
  23. radar(v1)
  24. if __name__ == '__main__':
  25. main()

image.png

7、树形图

  1. from pyecharts.charts import Tree
  2. from pyecharts import options as opts
  3. def draw(data):
  4. tree = Tree()
  5. tree.add('', data)
  6. tree.set_global_opts(title_opts=opts.TitleOpts(title="title"))
  7. tree.render('xxx.html')
  8. def main():
  9. # children数据类型为列表;可以为每个节点写入数值(value)
  10. data = [
  11. {"children": [{"name": "B", "value": "1"},
  12. {"children": [{"children": [{"name": "I"}], "name": "E"}, {"name": "F"}],
  13. "name": "C"},
  14. {"children": [{"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, {"name": "H"}],
  15. "name": "D"}],
  16. "name": "A"}
  17. ]
  18. draw(data)
  19. if __name__ == '__main__':
  20. main()

image.png

8、 主题河流图

  1. from pyecharts import options as opts
  2. from pyecharts.charts import ThemeRiver
  3. def draw(data, data_list):
  4. tr = ThemeRiver()
  5. # series_name各数据名称;data数据数值;SingleAxisOpts河流图位置
  6. tr.add(series_name=data_list, data=data,
  7. singleaxis_opts=opts.SingleAxisOpts(pos_top="50", pos_bottom="50", type_="time"))
  8. # TooltipOpts设置辅助线;LegendOpts设置图例位置
  9. tr.set_global_opts(title_opts=opts.TitleOpts(title="title"),
  10. tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"),
  11. legend_opts=opts.LegendOpts(pos_top=0))
  12. tr.render('xxx.html')
  13. def main():
  14. data = [['2015/11/08', 35, 'TY'],
  15. ['2015/11/09', 36, 'TY'],
  16. ['2015/11/10', 37, 'TY'],
  17. ['2015/11/11', 22, 'TY'],
  18. ['2015/11/12', 24, 'TY'],
  19. ['2015/11/13', 26, 'TY'],
  20. ['2015/11/14', 34, 'TY'],
  21. ['2015/11/15', 21, 'TY'],
  22. ['2015/11/16', 18, 'TY'],
  23. ['2015/11/17', 45, 'TY'],
  24. ['2015/11/18', 32, 'TY'],
  25. ['2015/11/19', 35, 'TY'],
  26. ['2015/11/20', 30, 'TY'],
  27. ['2015/11/21', 28, 'TY'],
  28. ['2015/11/22', 27, 'TY'],
  29. ['2015/11/23', 26, 'TY'],
  30. ['2015/11/24', 15, 'TY'],
  31. ['2015/11/25', 30, 'TY'],
  32. ['2015/11/26', 35, 'TY'],
  33. ['2015/11/27', 42, 'TY'],
  34. ['2015/11/28', 42, 'TY'],
  35. ['2015/11/08', 21, 'SS'],
  36. ['2015/11/09', 25, 'SS'],
  37. ['2015/11/10', 27, 'SS'],
  38. ['2015/11/11', 23, 'SS'],
  39. ['2015/11/12', 24, 'SS'],
  40. ['2015/11/13', 21, 'SS'],
  41. ['2015/11/14', 35, 'SS'],
  42. ['2015/11/15', 39, 'SS'],
  43. ['2015/11/16', 40, 'SS'],
  44. ['2015/11/17', 36, 'SS'],
  45. ['2015/11/18', 33, 'SS'],
  46. ['2015/11/19', 43, 'SS'],
  47. ['2015/11/20', 40, 'SS'],
  48. ['2015/11/21', 34, 'SS'],
  49. ['2015/11/22', 28, 'SS'],
  50. ['2015/11/23', 26, 'SS'],
  51. ['2015/11/24', 37, 'SS'],
  52. ['2015/11/25', 41, 'SS'],
  53. ['2015/11/26', 46, 'SS'],
  54. ['2015/11/27', 47, 'SS'],
  55. ['2015/11/28', 41, 'SS'],
  56. ['2015/11/08', 10, 'QG'],
  57. ['2015/11/09', 15, 'QG'],
  58. ['2015/11/10', 35, 'QG'],
  59. ['2015/11/11', 38, 'QG'],
  60. ['2015/11/12', 22, 'QG'],
  61. ['2015/11/13', 16, 'QG'],
  62. ['2015/11/14', 7, 'QG'],
  63. ['2015/11/15', 2, 'QG'],
  64. ['2015/11/16', 17, 'QG'],
  65. ['2015/11/17', 33, 'QG'],
  66. ['2015/11/18', 40, 'QG'],
  67. ['2015/11/19', 32, 'QG'],
  68. ['2015/11/20', 26, 'QG'],
  69. ['2015/11/21', 35, 'QG'],
  70. ['2015/11/22', 40, 'QG'],
  71. ['2015/11/23', 32, 'QG'],
  72. ['2015/11/24', 26, 'QG'],
  73. ['2015/11/25', 22, 'QG'],
  74. ['2015/11/26', 16, 'QG'],
  75. ['2015/11/27', 22, 'QG'],
  76. ['2015/11/28', 10, 'QG'],
  77. ['2015/11/08', 10, 'DD'],
  78. ['2015/11/09', 15, 'DD'],
  79. ['2015/11/10', 35, 'DD'],
  80. ['2015/11/11', 38, 'DD'],
  81. ['2015/11/12', 22, 'DD'],
  82. ['2015/11/13', 16, 'DD'],
  83. ['2015/11/14', 7, 'DD'],
  84. ['2015/11/15', 2, 'DD'],
  85. ['2015/11/16', 17, 'DD'],
  86. ['2015/11/17', 33, 'DD'],
  87. ['2015/11/18', 4, 'DD'],
  88. ['2015/11/19', 32, 'DD'],
  89. ['2015/11/20', 26, 'DD'],
  90. ['2015/11/21', 35, 'DD'],
  91. ['2015/11/22', 40, 'DD'],
  92. ['2015/11/23', 32, 'DD'],
  93. ['2015/11/24', 26, 'DD'],
  94. ['2015/11/25', 22, 'DD'],
  95. ['2015/11/26', 16, 'DD'],
  96. ['2015/11/27', 22, 'DD'],
  97. ['2015/11/28', 10, 'DD']]
  98. data_list = ['TY', 'SS', 'QG', 'DD']
  99. draw(data, data_list)
  100. if __name__ == '__main__':
  101. main()

image.png

9、 折柱混合图

  1. from pyecharts.charts import Bar, Line
  2. from pyecharts import options as opts
  3. x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  4. a = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
  5. b = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
  6. bar = Bar()
  7. bar.add_xaxis(xaxis_data=x)
  8. bar.add_yaxis(series_name="蒸发量", y_axis=a, label_opts=opts.LabelOpts(is_show=False))
  9. # 增加一个y轴用于折线图的数据展示
  10. # splitline_opts背景辅助线
  11. bar.extend_axis(yaxis=opts.AxisOpts(type_="value", min_=0, max_=25, interval=5,
  12. axislabel_opts=opts.LabelOpts(formatter="{value}°C"),
  13. splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)),
  14. axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color='#d14a61'))
  15. )
  16. )
  17. # 左侧y轴及全局设置
  18. bar.set_global_opts(title_opts=opts.TitleOpts(title="2020年每月天气概况"),
  19. tooltip_opts=opts.TooltipOpts(is_show=True, trigger="axis", axis_pointer_type="cross"), # 显示提示框、鼠标辅助线
  20. xaxis_opts=opts.AxisOpts(name_location="middle", name_gap=30),
  21. yaxis_opts=opts.AxisOpts(type_="value", min_=0, max_=210, interval=42,
  22. axislabel_opts=opts.LabelOpts(formatter="{value}ml"),
  23. axistick_opts=opts.AxisTickOpts(is_show=True),
  24. axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(color='#5793f3'))
  25. )
  26. )
  27. # 折线图
  28. line = Line()
  29. line.add_xaxis(xaxis_data=x)
  30. # z大于0可以使折现位于柱状上方
  31. line.add_yaxis(series_name="平均温度", yaxis_index=1, y_axis=b, label_opts=opts.LabelOpts(is_show=True), z=10)
  32. # 将两图写入一个坐标系中
  33. bar.overlap(line)
  34. bar.render('xxx.html')

image.png

10、 工具(补充)

(1)添加区域缩放

  1. '''
  2. 以折线图为例
  3. '''
  4. line.set_global_opts(
  5. title_opts=opts.TitleOpts(title='name'),
  6. datazoom_opts=opts.DataZoomOpts(
  7. is_show=True
  8. , range_start=0 # 默认开始位置(最小0)
  9. , range_end=100 # 默认结束位置(最大100)
  10. , orient='horizontal' # 横向horizontal和纵向vertical
  11. )
  12. )

image.png

(2)添加工具栏

  1. '''
  2. 以折线图为例
  3. '''
  4. line.set_global_opts(toolbox_opts=opts.ToolboxOpts(is_show=True))

image.png

 (3)标记最大最小值

  1. '''
  2. 以折线图为例
  3. '''
  4. line.set_series_opts(markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
  5. opts.MarkPointItem(type_="min", name="最小值")]))

image.png

(4)平均值标线

  1. '''
  2. 以折线图为例
  3. '''
  4. line.add_yaxis(
  5. "线名称", ylist, is_smooth=True,
  6. markline_opts=opts.MarkLineOpts(
  7. data=[opts.MarkLineItem(type_='average', name='平均值')]
  8. , linestyle_opts=opts.LineStyleOpts(
  9. type_='dashed' # 点状
  10. , opacity=0.5 # 透明度0-1值越小越透明
  11. , color='black'
  12. )
  13. )
  14. )

image.png

 (5)保存为其他格式到本地

  1. '''
  2. 已折线图为例,除备注两行外其他代码与之前相同(注意需要引入两个库)
  3. '''
  4. from pyecharts import options
  5. from pyecharts.charts import Line
  6. from snapshot_selenium import snapshot
  7. from pyecharts.render import make_snapshot
  8. def draw(xlist, ylist):
  9. line = Line(init_opts=options.InitOpts(bg_color='#ffffff')) # 背景色更改为白色,默认为透明
  10. line.add_xaxis(xlist)
  11. line.add_yaxis("线名称", ylist, is_smooth=True)
  12. line.set_global_opts(title_opts=options.TitleOpts(title='name'))
  13. make_snapshot(snapshot, line.render(), "xxx.png") # 生成png格式文件
  14. def main():
  15. xlist = ['一', '二', '三', '四', '五']
  16. ylist = [3, 9, 15, 21, 27]
  17. draw(xlist, ylist)
  18. if __name__ == '__main__':
  19. main()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/329381
推荐阅读
相关标签
  

闽ICP备14008679号