• 世界这么大,Python 也想去看看


    把时间线拉回到 2015 年 4 月 13 日,一位河南省实验中学的心里老师在辞职信上写下了「世界那么大,我想去看看」这句话,后来爆红网络,我想这位心里老师当时写这句话的时候肯定没想到这句只有十个字的话,成为了当年的网络经典语录,成为了我们每个人对那个年代的回忆。

    这句话直接导致了当年很多营销号出来炒作概念,乱七八糟瞎炒一气,蛊惑了不知道多少年轻人当年自认为潇洒的裸辞掉工作,一个人跑出去看世界。

    可惜的是当年脑子一热裸辞掉工作跑出去看世界的人,不知道有多少人还能稳定的着陆。忽然想起来不知道在哪看到的一句话 「不是工作需要你,而是你需要一份工作」

    大家都是成年人,做决定前最好能自己多思考几分钟,仔细想想自己是否能承担后果。

    我们想要出去看世界代价有点高,但是 Python 想看世界不是一般的简单,我们只需要用到一个 pyecharts ,就可以让 Python 想看啥看啥。

    安装

    Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。

    pyecharts 的安装非常简单,只需要一句话:

    pip install pyecharts
    

    pyecharts 环境:

    • Python3.6+

    pyecharts 文档地址:https://pyecharts.org/#/zh-cn/intro

    pyecharts 示例代码:https://github.com/pyecharts/pyecharts-gallery

    看世界

    pyecharts 为我们提供了地图组件 Map ,我们可以直接使用 Map 组件来看世界。

    接下来我们来看下如何使用 pyecharts 的 Map 组件来画地图。

    以下示例均参考自官方示例

    Map 组件的使用非常简单,我们可以先看下世界地图:

    import pyecharts.options as opts
    from pyecharts.charts import MapGlobe
    from pyecharts.faker import POPULATION
    
    
    data = [x for _, x in POPULATION[1:]]
    low, high = min(data), max(data)
    
    c = (
        MapGlobe()
        .add_schema()
        .add(
            maptype="world",
            series_name="World Population",
            data_pair=POPULATION[1:],
            is_map_symbol_show=False,
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            visualmap_opts=opts.VisualMapOpts(
                min_=low,
                max_=high,
                range_text=["max", "min"],
                is_calculable=True,
                range_color=["lightskyblue", "yellow", "orangered"],
            )
    
        )
        .render("map_globe.html")
    )
    

    世界看的不想看了还能看看我们伟大的祖国,代码也很简单:

    from pyecharts import options as opts
    from pyecharts.charts import Map
    from pyecharts.faker import Faker
    from pyecharts.globals import ThemeType
    
    c = (
        Map(init_opts=opts.InitOpts(
            theme=ThemeType.DARK,
            bg_color='#404a59'
        ))
        .add(
            "中国地图",
            [list(z) for z in zip(Faker.provinces, Faker.values())],
            "china"
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中国地图-示例"),
            visualmap_opts=opts.VisualMapOpts(),
        )
        .render("china_map.html")
    )
    

    再来一个我生活的城市上海的地图:

    from pyecharts import options as opts
    from pyecharts.charts import Map
    from pyecharts.globals import ThemeType
    
    shanghai_list = ['黄浦区', '徐汇区', '长宁区', '静安区', '普陀区', '虹口区', '杨浦区', '闵行区', '宝山区', '嘉定区', '金山区', '松江区', '青浦区', '奉贤区', '崇明区', '浦东新区']
    
    shanghai_people = [65.38, 108.44, 69.4, 106.28, 128.19, 79.7, 131.27, 254.35, 204.23, 158.89, 80.5, 176.22, 121.9, 115.2, 68.81, 555.02]
    
    BAIDU_LINK='https://baike.baidu.com/item/%E4%B8%8A%E6%B5%B7%E8%A1%8C%E6%94%BF%E5%8C%BA%E5%88%92/7426389?fr=aladdin'
    
    c = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.DARK, bg_color='#404a59', width='1600px', height='900px'))
        .add("上海市-常住人口", [list(z) for z in zip(shanghai_list, shanghai_people)], "上海")
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="上海地图-常住人口(单位:万人)",
                subtitle="常住人口数据来自百度百科",
                subtitle_link=BAIDU_LINK,
            ),
            visualmap_opts=opts.VisualMapOpts()
        )
        .render("map_shanghai.html")
    )
    

    看完 2D 的有没有觉得不过瘾, Map 组件还未我们提供了 3D 地图的组件 Map3D :

    from pyecharts import options as opts
    from pyecharts.charts import Map3D
    from pyecharts.globals import ThemeType
    from pyecharts.globals import ChartType
    
    from pyecharts.globals import CurrentConfig
    CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"
    
    c = (
        Map3D(init_opts=opts.InitOpts(
            theme=ThemeType.DARK,
            bg_color='#404a59',
            width='1600px',
            height='900px'
        ))
        .add_schema(
            itemstyle_opts=opts.ItemStyleOpts(
                color="#313c48",
                opacity=1,
                border_width=0.8,
                border_color="#000",
            ),
            map3d_label=opts.Map3DLabelOpts(
                is_show=True,
                text_style=opts.TextStyleOpts(
                    color="#fff", font_size=16, background_color="rgba(0,0,0,0)"
                ),
            ),
            emphasis_label_opts=opts.LabelOpts(is_show=True),
            light_opts=opts.Map3DLightOpts(
                main_color="#fff",
                main_intensity=1.2,
                is_main_shadow=False,
                main_alpha=55,
                main_beta=10,
                ambient_intensity=0.3,
            ),
        )
        .add(series_name="", data_pair="", maptype=ChartType.MAP3D)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="全国行政区划地图"),
            visualmap_opts=opts.VisualMapOpts(is_show=False),
            tooltip_opts=opts.TooltipOpts(is_show=True),
        )
        .render("map3d_china.html")
    )
    

    是不是很简单,这些地图我都没有配数据,可以配置上数据做一些看板的展示。

    还有一点, pyecharts 的所需要的静态资源,会默认访问 https://assets.pyecharts.org/ 获取(可以查看生成的 html 文件):

    如果网络条件不好,地图会展示不出来,我们可以使用官方为我们提供的本地服务:

    需要先获取 pyecharts-assets 项目

    git clone https://github.com/pyecharts/pyecharts-assets.git
    

    然后进入 pyecharts-assets 目录启动 HTTP file server :

    cd pyecharts-assets
    python -m http.server
    
    # Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
    # 默认会在本地 8000 端口启动一个文件服务器
    

    然后我们在代码中配置 pyecharts 全局 HOST:

    # 只需要在顶部声明 CurrentConfig.ONLINE_HOST 即可
    from pyecharts.globals import CurrentConfig
    CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"
    
    # 接下来所有图形的静态资源文件都会来自刚启动的服务器
    

    添加完成后的 html 文件生成后应该是这样的:

    这时我们再打开页面,可以看到页面基本上是秒刷新。

    需要获取源码的同学可以在公众号后台回复「看世界」获取。

  • 相关阅读:
    MySQL主从复制集群添加slave节点
    GTID主从 与 传统主从复制
    20201207总结
    202011051 每周例行报告
    202011121 每周例行报告
    202010081 每周例行报告
    202011261 每周例行报告
    202010153 每周例行报告
    202010291 每周例行报告
    202011201 每周例行报告
  • 原文地址:https://www.cnblogs.com/babycomeon/p/12886548.html
Copyright © 2020-2023  润新知