美文网首页
Python数据可视

Python数据可视

作者: Lijy_李俊阳 | 来源:发表于2020-05-13 18:23 被阅读0次

    1.显现用到的包

    Pyecharts 是一个用于生成 Echarts 图表的类库。常规的Echarts 是由百度开源的一个数据可视化 JS 库,主要用于数据可视化。简单来说,Pyecharts是一款将python与echarts结合的强大的数据可视化工具。

    使用 Pyecharts 可以生成独立的网页,也可以在 flask , Django 中集成使用。

    项目介绍:
    http://pyecharts.herokuapp.com/

    项目源码:
    https://github.com/pyecharts/pyecharts

    2.Pyecharts安装

    # 安装 v1 以上版本
    $ pip install pyecharts -U
    
    # 如果需要安装 0.5.11 版本的开发者,可以使用
    # pip install pyecharts==0.5.11
    

    PS: 这里要专门说明一下,自从 0.3.2 开始,为了缩减项目本身的体积以及维持 pyecharts 项目的轻量化运行,pyecharts 将不再自带地图 js 文件。如用户需要用到地图图表(Geo、Map),可自行安装对应的地图文件包。

    # 通过pip命令进行安装
    pip install echarts-countries-pypkg
    pip install echarts-china-provinces-pypkg
    pip install echarts-china-cities-pypkg
    

    3.实践

    1.柱状图
    from pyecharts.charts import Bar
    from pyecharts import options as opts
    
    # V1 版本开始支持链式调用
    bar = (
        Bar()
        .add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
        .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
        .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
        .set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
    )
    bar.render()
    
    image.png
    2.Pie饼状图
    from pyecharts import options as opts
    from pyecharts.charts import Pie
    from pyecharts.faker import Faker
    
    pie = (
        Pie()
        .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
        .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie-设置颜色"))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    
    pie.render()
    
    image.png
    4. 仪表盘
    from pyecharts import options as opts
    from pyecharts.charts import Gauge
    
    g = (
        Gauge()
        .add("", [("完成率", 99.6)])
        .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
    
    )
    g.render()
    
    image.png
    5.折线图
    import pyecharts.options as opts
    from pyecharts.charts import Line
    from pyecharts.faker import Faker
    
    c = (
      Line()
      .add_xaxis(Faker.choose())
      .add_yaxis("商家A", Faker.values(), is_smooth=True)
      .add_yaxis("商家B", Faker.values(), is_smooth=True)
      .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth"))
    
    )
    c.render()
    
    image.png
    6.K线图
    from pyecharts import options as opts
    from pyecharts.charts import Kline
    
    data = [
        [2320.26, 2320.26, 2287.3, 2362.94],
        [2300, 2291.3, 2288.26, 2308.38],
        [2295.35, 2346.5, 2295.35, 2345.92],
        [2347.22, 2358.98, 2337.35, 2363.8],
        [2360.75, 2382.48, 2347.89, 2383.76],
        [2383.43, 2385.42, 2371.23, 2391.82],
        [2377.41, 2419.02, 2369.57, 2421.15],
        [2425.92, 2428.15, 2417.58, 2440.38],
        [2411, 2433.13, 2403.3, 2437.42],
        [2432.68, 2334.48, 2427.7, 2441.73],
        [2430.69, 2418.53, 2394.22, 2433.89],
        [2416.62, 2432.4, 2414.4, 2443.03],
        [2441.91, 2421.56, 2418.43, 2444.8],
        [2420.26, 2382.91, 2373.53, 2427.07],
        [2383.49, 2397.18, 2370.61, 2397.94],
        [2378.82, 2325.95, 2309.17, 2378.82],
        [2322.94, 2314.16, 2308.76, 2330.88],
        [2320.62, 2325.82, 2315.01, 2338.78],
        [2313.74, 2293.34, 2289.89, 2340.71],
        [2297.77, 2313.22, 2292.03, 2324.63],
        [2322.32, 2365.59, 2308.92, 2366.16],
        [2364.54, 2359.51, 2330.86, 2369.65],
        [2332.08, 2273.4, 2259.25, 2333.54],
        [2274.81, 2326.31, 2270.1, 2328.14],
        [2333.61, 2347.18, 2321.6, 2351.44],
        [2340.44, 2324.29, 2304.27, 2352.02],
        [2326.42, 2318.61, 2314.59, 2333.67],
        [2314.68, 2310.59, 2296.58, 2320.96],
        [2309.16, 2286.6, 2264.83, 2333.29],
        [2282.17, 2263.97, 2253.25, 2286.33],
        [2255.77, 2270.28, 2253.31, 2276.22],
    ]
    
    
    k = (
        Kline()
        .add_xaxis(["2017/7/{}".format(i + 1) for i in range(31)])
        .add_yaxis("k线图", data)
        .set_global_opts(
            yaxis_opts=opts.AxisOpts(is_scale=True),
            xaxis_opts=opts.AxisOpts(is_scale=True),
            title_opts=opts.TitleOpts(title="K线图-基本示例"),
        )
    
    )
    k.render()
    
    image.png
    7.地图
    from pyecharts import options as opts
    from pyecharts.charts import Map
    from pyecharts.faker import Faker
    
    map = (
        Map()
        .add("中国地图", [list(z) for z in zip(Faker.provinces, Faker.values())], "china")
        .set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"))
    )
    map.render()
    
    image.png

    相关文章

      网友评论

          本文标题:Python数据可视

          本文链接:https://www.haomeiwen.com/subject/wqfonhtx.html