• python中第三方库pyecharts的使用


            与pyecharts有关的两个网站:官方网站:pyecharts - A Python Echarts Plotting Library built with love. ,画廊功能的网站:DocumentDescriptionhttps://gallery.pyecharts.org/#/在画廊网站中可以查看各个图的实例

            pyecharts的作用:用来做数据图表

    做一个图的步骤:

            1.导包

            2.创建一个图对象

            3.添加数据

            4.设置全局配置项

            5.通过render方法将代码生成图像

    1.折线图

    1. # 使用 ab173懒人程序员工具做json数据分析
    2. # 1.导包
    3. from pyecharts.charts import Line
    4. # TitleOpts:控制图标题的模块 设置全局选项时需要引用的模块
    5. from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
    6. # 2.创建一个折线图对象
    7. line = Line()
    8. # 3.给折线图对象添加x轴的数据
    9. line.add_xaxis(['中国', '美国', '英国'])
    10. # 4.给折线图对象添加y轴对象
    11. line.add_yaxis('GDP', [30, 20, 10])
    12. # 5.设置全局配置项,通过 line.set_global_opts 来设置
    13. line.set_global_opts(
    14. # 标题配置
    15. # 参数列表:title:标题,pos_left:x轴横向位置,pos_bottom:y轴竖向位置
    16. title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"),
    17. # 图例控制:默认是开启的
    18. legend_opts=LegendOpts(is_show=True),
    19. # 工具箱
    20. toolbox_opts=ToolboxOpts(is_show=True),
    21. # 视觉映射
    22. visualmap_opts=VisualMapOpts(is_show=True)
    23. )
    24. # 6.通过render方法,将代码生成图像
    25. line.render('折线图.html')

             【效果】

     

    2. 地图

    1. # 1.导包
    2. from pyecharts.charts import Map
    3. from pyecharts.options import VisualMapOpts
    4. # 2.准备地图对象
    5. map = Map()
    6. # 3.准备数据,所用数据都是列表嵌套元组
    7. data = [
    8. ("北京", 99),
    9. ("上海", 199),
    10. ("湖南", 299),
    11. ("广东", 399),
    12. ("台湾", 499),
    13. ]
    14. # 4.添加数据
    15. map.add("测试地图", data, "china")
    16. # 5.设置全局选项
    17. map.set_global_opts(
    18. # 视图功能
    19. visualmap_opts=VisualMapOpts(
    20. # 该参数设置视图开启
    21. is_show=True,
    22. # 该参数改变视图模式
    23. is_piecewise=True,
    24. # 颜色和表签的设置
    25. pieces=[
    26. {"min": 1, "max": 9, "label": "1-9", "color": "#CCFFFF"},
    27. {"min": 10, "max": 299, "label": "10-299", "color": "#CC6666"},
    28. {"min": 300, "max": 500, "label": "300-500", "color": "#990033"}
    29. ]
    30. )
    31. )
    32. # 6.绘图
    33. map.render("中国部分地图.html")

            【效果】

     3. 柱状图

    1. # 1.导包
    2. from pyecharts.charts import Bar
    3. from pyecharts.options import LabelOpts
    4. # 2.创建对象
    5. bar = Bar()
    6. # 3.添加数据
    7. bar.add_xaxis(['中国', '美国', '英国'])
    8. bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right")) # position可以设置参数位置
    9. # 反转x和y轴
    10. bar.reversal_axis()
    11. # 4.绘图
    12. bar.render('基础柱状图.html')

            【效果】

        4. 基础时间线柱状图

    1. # 1.导包
    2. from pyecharts.charts import Bar, Timeline # 导入时间模块
    3. from pyecharts.options import LabelOpts
    4. from pyecharts.globals import ThemeType # 导入时间线主题
    5. # 2.创建对象
    6. bar1 = Bar()
    7. # 3.添加数据
    8. bar1.add_xaxis(['中国', '美国', '英国'])
    9. bar1.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right")) # position可以设置参数位置
    10. bar1.reversal_axis()
    11. bar2 = Bar()
    12. bar2.add_xaxis(['中国', '美国', '英国'])
    13. bar2.add_yaxis("GDP", [50, 30, 20], label_opts=LabelOpts(position="right")) # position可以设置参数位置
    14. bar2.reversal_axis()
    15. bar3 = Bar()
    16. bar3.add_xaxis(['中国', '美国', '英国'])
    17. bar3.add_yaxis("GDP", [70, 50, 30], label_opts=LabelOpts(position="right")) # position可以设置参数位置
    18. bar3.reversal_axis()
    19. # 创建时间线对象,传入一个字典设置主题
    20. timeline = Timeline({"theme": ThemeType.LIGHT})
    21. # 在时间线添加柱状图对象
    22. timeline.add(bar1, "点1")
    23. timeline.add(bar2, "点2")
    24. timeline.add(bar3, "点3")
    25. # 设置自动播放
    26. timeline.add_schema(
    27. play_interval=1000, # 设置自动播放的时间间隔,单位毫秒
    28. is_timeline_show=True, # 是否在自动播放的时候,显示时间线
    29. is_auto_play=True, # 是否自动播放
    30. is_loop_play=True # 是否循环自动播放
    31. )
    32. # 4.绘图是使用时间线绘图,而不是bar对象
    33. timeline.render("基础时间线柱状图.html")

            【效果】

     

  • 相关阅读:
    QML9、输入元素
    elmentui el-select下拉输入不清空已选值
    3天精通Postman---动态参数&断言&CSV数据驱动&Mock Server
    【实践篇】Redis最强Java客户端(三)之Redisson 7种分布式锁使用指南
    vue学习之列表渲染
    Python实战——Selenium与iframe结合应用
    [cpp primer随笔] 11. 内联函数与constexpr函数
    Uniapp导航栏右侧自定义图标文字按钮
    【威联通】共享文件夹设置
    直面秋招:非科班生背水一战,最终拿下阿里字节等大厂offer
  • 原文地址:https://blog.csdn.net/weixin_62859191/article/details/126510865