当前位置: 移动技术网 > IT编程>脚本编程>Python > Python数据分析之画图力气pyecharts 制作3D图像!

Python数据分析之画图力气pyecharts 制作3D图像!

2020年04月23日  | 移动技术网IT编程  | 我要评论

深圳吧,泰前总理英拉迎50岁生日,沈佳润

python 功能真的很强,强大到让人吃惊,它能做的事囊括爬虫、数据分析、数据可视化、游戏等等各方面,这些功能在实际的使用中应用广泛,开发程序讲究页面的美观与炫酷效果, 今天的文章将给各位读者朋友们带来不一样的视觉盛宴,感兴趣的朋友欢迎一起尝试。

写在前面的话:在之前的文章 python 图表利器 pyecharts 中有介绍了 pyecharts 的安装及使用,详细教程请到  学习

pyecharts 功能很强大,只需要导入相应的模块就配置相应的选项即可生成对应的超文本文件,使用浏览器访问即可!具体实例请见下文

盛宴1-2d世界地图

先来个 2d 的瞅瞅~

实现代码如下:

from pyecharts import options as opts
from pyecharts.charts import map
from pyecharts.faker import faker

c = (
    map(init_opts=opts.initopts(width='1500px', height='1200px',bg_color='#e0eeee'))
    # 加载世界地图实例
    .add("世界地图", [list(z) for z in zip(faker.country, faker.values())], "world")
   # 不显示地图标志
    .set_series_opts(label_opts=opts.labelopts(is_show=false))
    .set_global_opts(
        # 配置项标题设置
        title_opts=opts.titleopts(title="世界地图示例"),
        visualmap_opts=opts.visualmapopts(max_=200)
    )
    # 生成超文本文件
    .render("world_map.html")
)

盛宴2-中国3d地图

通过导入 map3d 等实现中国地图的 3d 呈现:

实现代码如下:

from pyecharts import options as opts
from pyecharts.charts import map3d
from pyecharts.globals import charttype

c = (
    map3d(init_opts=opts.initopts(width='1300px', height='1300px',bg_color='#ebebeb'))

    .add_schema(
        itemstyle_opts=opts.itemstyleopts(
            color="#cdba96",
            opacity=1,
            border_width=0.8,
            border_color="rgb(62,215,213)",
        ),
        map3d_label=opts.map3dlabelopts(
            is_show=true,
            text_style=opts.textstyleopts(
                color="#104e8b", font_size=16, background_color="rgba(0,0,0,0)"
            ),
        ),
        emphasis_label_opts=opts.labelopts(is_show=true),
        light_opts=opts.map3dlightopts(
            main_color="#ffebcd",
            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_base.html")
)

盛宴3-贵州地图

现在用另一种方式来实现我家乡的地图,一起来一睹为快~

代码实现如下:

# 写入省份内各地区经纬度
example_data = [
    [[106.70722,26.59820, 1000],[106.63024, 26.64702, 1000]],
    [[104.83023, 26.59336], [106.92723, 27.72545]],
    [[105.30504, 27.29847], [107.52034, 26.29322]],
    [[107.89868, 26.52881], [104.948571, 25.077502]],
    [[105.9462, 26.25367], [109.18099, 27.69066]],
]
# 添加 3d 地图
c = (
    map3d(init_opts=opts.initopts(width='1200px', height='1200px'))
    .add_schema(
        maptype="贵州",
        itemstyle_opts=opts.itemstyleopts(
            color="rgb(5,101,123)",
            opacity=1,
            border_width=0.8,
            border_color="rgb(62,215,213)",
        ),
        light_opts=opts.map3dlightopts(
            main_color="#fff",
            main_intensity=1.2,
            is_main_shadow=true,
            main_alpha=55,
            main_beta=10,
            ambient_intensity=0.3,
        ),
        view_control_opts=opts.map3dviewcontrolopts(center=[-10, 0, 10]),
        post_effect_opts=opts.map3dposteffectopts(is_enable=true),

    )
    .add(
        series_name="",
        data_pair=example_data,
        type_=charttype.lines3d,
        effect=opts.lines3deffectopts(
            is_show=true,
            period=4,
            trail_width=3,
            trail_length=0.5,
            trail_color="#f00",
            trail_opacity=1,
        ),
        label_opts=opts.labelopts(is_show=true),
    )
    .set_global_opts(title_opts=opts.titleopts(title="map3d-guizhou3d"))
    .render("guizhou_map_3d.html")
)

盛宴4-地球村实现

一起来看看旋转的地球吧^^

实现代码如下:

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(init_opts=opts.initopts(width='1000px', height='1000px',bg_color='#fffafa',))
    .add_schema()
    .add(
        maptype="world",
        series_name="world population",
        data_pair=population[1:],
        is_map_symbol_show=true,
        label_opts=opts.labelopts(is_show=true),
    )
    .set_global_opts(
        title_opts=opts.titleopts(title="3d 地球示例"),
        # 设置地球属性
        visualmap_opts=opts.visualmapopts(
            min_=low,
            max_=high,
            range_text=["max", "min"],
            is_calculable=true,
            range_color=["lightskyblue", "yellow", "orangered"],
        )
    )
    .render("world_map_3d.html")
)

总结

希望今天的分享能给大家带来不一样的视觉享受,同时伙伴们也别忘了要多多实践。 实践是检验真理的唯一标准!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网