python中pyecharts绘制地图

pyecharts中的Geo 地理坐标系组件用于地图的绘制,可直接使用全国的城市信息。

使用前先安装相关地图扩展包:

pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-cities-pypkg

1,全国地图:

from pyecharts import Geo

data = [
    ("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15)
]

data2 = [
    ("北京", 9), ("上海", 12), ("拉萨", 12), ("重庆", 12), ("乌鲁木齐", 14), ("昆明", 15), ("西宁", 22), ("兰州", 17)
]
geo = Geo(
    "全国主要城市空气质量",
    "data from pm2.5",
    title_color="#fff",
    title_pos="center",
    width=1200,
    height=600,
    background_color="#404a59",
)
attr, value = geo.cast(data)
attr2, value2 = geo.cast(data2)
geo.add(
    "",
    attr,
    value,
    type="effectScatter",
    # is_random=True,
    symbol_size=8,
    effect_scale=5,
    effect_period=3.5)
geo.add(
    "",
    attr2,
    value2,
    type="effectScatter",
    is_random=True,
    symbol="pin",
    symbol_size=10,
    effect_scale=5,
    effect_period=2.5,
    is_more_utils=True)
geo.show_config()
geo.render(path="Geo.html")

结果如图:全国地图中右上角的那块有个小区域比较特别,在黑龙江省和内蒙古自治区的挨着那片区域,是黑龙江省的“飞地”——加格达奇。http://wemedia.ifeng.com/91014790/wemedia.shtml

 python中pyecharts绘制地图_第1张图片

2,使用Visualmap的代码:

from pyecharts import Geo

data = [
    ("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15),
    ("北京", 9), ("上海", 12), ("拉萨", 12), ("重庆", 12), ("乌鲁木齐", 14), ("昆明", 15), ("西宁", 22), ("兰州", 17)
]
geo = Geo(
    "全国主要城市空气质量",
    "data from pm2.5",
    title_color="#fff",
    title_pos="left",
    width=1200,
    height=600,
    background_color="#404a59",
)
attr, value = geo.cast(data)
geo.add(
    " ", # 注意与""的区别,在图顶部中间的scatter点
    attr,
    value,
    type="effectScatter",
    is_random=True,
    # symbol="pin",
    symbol_size=10,
    effect_scale=5,
    effect_period=2.5,
    is_more_utils=True,
    is_visualmap=True,
    visual_range=[0, 25],
    visual_text_color="#fff",
)
geo.show_config()
geo.render(path="Geo.html")

结果如图:

python中pyecharts绘制地图_第2张图片

3,广东省地图:

from pyecharts import Geo

data = [("汕头市", 50), ("汕尾市", 60), ("揭阳市", 35), ("阳江市", 44), ("肇庆市", 72),
        ("湛江市", 13), ("韶关市", 88), ("广州市", 12), ("佛山市", 24), ("清远市", 66)]
geo = Geo(
    "广东城市空气质量",
    "data from pm2.5",
    title_color="#fff",
    title_pos="center",
    width=1200,
    height=600,
    background_color="#404a59",
)
attr, value = geo.cast(data)
geo.add(
    "",
    attr,
    value,
    maptype="广东",
    type="effectScatter",
    # is_random=True,
    effect_scale=5,
    # is_legend_show=False,
    is_visualmap=True,
    visual_range=[0, 100],
    visual_text_color='#000',
    # is_label_show=True,    # 这个显示的是市的维度
    is_more_utils=True,
)
geo.show_config()
geo.render(path="Geo.html")

结果如图:

 python中pyecharts绘制地图_第3张图片

4,北京市地图:

from pyecharts import Geo


data = [("海淀区", 20), ("丰台区", 29), ("西城区", 25), ("东城区", 24), ("房山区", 22),
        ("昌平区", 13), ("密云县", 11), ("怀柔区", 16), ("通州区", 15), ("大兴区", 9)]
geo = Geo(
    "北京城市空气质量",
    "data from pm2.5",
    title_color="#fff",
    title_pos="center",
    width=1200,
    height=600,
    background_color="#404a59",
)
attr, value = geo.cast(data)
geo.add(
    "",
    attr,
    value,
    maptype="北京",
    type="effectScatter",
    # is_random=True,
    effect_scale=5,
    # is_legend_show=False,
    is_visualmap=True,
    visual_range=[0, 30],
    visual_text_color='#000',
    # is_label_show=True,    # 这个显示的是市的维度
    is_more_utils=True,
)
geo.show_config()
geo.render(path="Geo.html")

 结果如图:

python中pyecharts绘制地图_第4张图片

注意:

is_label_show=True时,地图上显示的是每个市的维度,

每个市是按照经纬度进行定位的,第3个值才是数据值,其数据组织形式如下:

"data": [
                {
                    "name": "\u6d77\u6dc0\u533a",
                    "value": [
                        116.3,
                        39.95,
                        20
                    ]
                },
                {
                    "name": "\u4e30\u53f0\u533a",
                    "value": [
                        116.28,
                        39.85,
                        29
                    ]
                },
                {
                    "name": "\u897f\u57ce\u533a",
                    "value": [
                        116.37,
                        39.92,
                        25
                    ]
                },
                {
                    "name": "\u4e1c\u57ce\u533a",
                    "value": [
                        116.42,
                        39.93,
                        24
                    ]
                },
                {
                    "name": "\u623f\u5c71\u533a",
                    "value": [
                        116.13,
                        39.75,
                        22
                    ]
                },
                {
                    "name": "\u660c\u5e73\u533a",
                    "value": [
                        116.23,
                        40.22,
                        13
                    ]
                },
                {
                    "name": "\u5bc6\u4e91\u53bf",
                    "value": [
                        116.83,
                        40.37,
                        11
                    ]
                },
                {
                    "name": "\u6000\u67d4\u533a",
                    "value": [
                        116.63,
                        40.32,
                        16
                    ]
                },
                {
                    "name": "\u901a\u5dde\u533a",
                    "value": [
                        116.65,
                        39.92,
                        15
                    ]
                },
                {
                    "name": "\u5927\u5174\u533a",
                    "value": [
                        116.33,
                        39.73,
                        9
                    ]
                }
            ],

参考:

http://pyecharts.org/#/zh-cn/charts_base?id=geo%EF%BC%88%E5%9C%B0%E7%90%86%E5%9D%90%E6%A0%87%E7%B3%BB%EF%BC%89 

你可能感兴趣的:(Python3)