用pyecharts的map模块实现齐齐哈尔各个区县人口分布图2021/4/20

1、前言
1.1 这个是上课老师让练习的,所以就根据之前的方法改写+网上找各种方法写成的齐齐哈尔各个区县人口分布图
1.2 代码中有点注释可能是之前的,所以有的注释和当前题目要求不一定对应
2、代码
2.1 py文件

from pyecharts.charts import Geo
from pyecharts import options as opts
import csv
from pyecharts.charts import Map


from pyecharts.globals import ChartType

'''
1.1和1.2分别是对取csv文件的数据为列表和字典(这个只用到了1.1和1.3)
1.3列表转换为有数量的字典
'''
#1.1读取csv文件,把性别信息读取出来
def getInfo(filename,index):#index 参数代表从第几列开始读取
       lstdata=[]
       with open(filename,'r') as fr:
              reader=csv.reader(fr)
              for i in reader:
                  lstdata.append(i[index])
       return lstdata

#1.2读取csv文件,并把寝室号信息读取出来
def getValues(filename):
    lstsex = {
     }
    with open(filename,'r') as f:
        reader =csv.reader(f)
        for i in reader:
            lstsex[i[1]]=i[2]
    return lstsex

#1.3列表转换为有数量的字典
def listToDict(lstsex):
    wc = dict()#{}
    for i in range(len(lstsex)):
        word = lstsex[i]
        if word not in wc:
            wc[word] = 1
        else:
            wc[word] += 1
    return wc

def GeoMapTest(lstsex):
    x1=list(lstsex.keys())#字典转为列表需要使用list才能转成功
    #y是字典的value值,里面包含了每一个城市的学生数
    y1=list(lstsex.values())
    print(x1)
    print(y1)
   #  #人口分布地图
   # c=(Map().#"齐齐哈尔区县人口分布","齐齐哈尔",width=800,heigth=400
   #    add("齐齐哈尔",,visual_range=[1,10],maptype='齐齐哈尔',is_visualmap=True).render(path="齐齐哈尔各区县人口分布.html"))
   #  return c
    aa = [list(z) for z in zip(x1, y1)]
    map=Map()
    # #"齐齐哈尔区县人口分布","齐齐哈尔",width=800,heigth=400#,visual_range=[1,10]
    # #颜色二进制显示https://blog.csdn.net/shakespeare001/article/details/7816022
    c=(
        map.add("齐齐哈尔",aa,maptype='齐齐哈尔')
        .set_global_opts(
            visualmap_opts=opts.VisualMapOpts(
                is_piecewise=True,
                pieces=[
                    {
     "min": 0, "max": 5, "label": "0-5万", "color": "#CCFF99"},
                    {
     "min": 6, "max": 10, "label": "6-10万", "color": "#88AA00"},
                    {
     "min": 11, "max": 20, "label": "11-20万", "color": "#886600"},
                    {
     "min": 21, "max": 50, "label": "21-50万", "color": "#CC6600"},
                    {
     "min": 51, "max": 100, "label": "51-100万", "color": "#CC0000"}
                ]
            ),
            title_opts=opts.TitleOpts(title="齐齐哈尔各区县人口分布",subtitle="数据来源:中国统计年鉴(万人)",pos_right="center",pos_top="5%")
        )
    )
    c.render(path="齐齐哈尔各区县人口分布2.html")
    return c


GeoMapTest(getValues("../file/齐齐哈尔2021年各区县人口new2.csv"))

2.2 参考链接
map写法参考链接:
https://blog.csdn.net/qq_32532663/article/details/113864429

16进制颜色引用链接:
https://blog.csdn.net/shakespeare001/article/details/7816022

3、结果
3.1 运行图片
用pyecharts的map模块实现齐齐哈尔各个区县人口分布图2021/4/20_第1张图片
3.2 运行结果的HTML代码文件


<html>
<head>
    <meta charset="UTF-8">
    <title>Awesome-pyechartstitle>
            <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js">script>
        <script type="text/javascript" src="https://assets.pyecharts.org/assets/maps/hei1_long2_jiang1_qi2_qi2_ha1_er3.js">script>

head>
<body>
    <div id="1251ae6f601149799aa7fbde2f6c0659" class="chart-container" style="width:900px; height:500px;">div>
    <script>
        var chart_1251ae6f601149799aa7fbde2f6c0659 = echarts.init(
            document.getElementById('1251ae6f601149799aa7fbde2f6c0659'), 'white', {
      renderer: 'canvas'});
        var option_1251ae6f601149799aa7fbde2f6c0659 = {
      
    "animation": true,
    "animationThreshold": 2000,
    "animationDuration": 1000,
    "animationEasing": "cubicOut",
    "animationDelay": 0,
    "animationDurationUpdate": 300,
    "animationEasingUpdate": "cubicOut",
    "animationDelayUpdate": 0,
    "color": [
        "#c23531",
        "#2f4554",
        "#61a0a8",
        "#d48265",
        "#749f83",
        "#ca8622",
        "#bda29a",
        "#6e7074",
        "#546570",
        "#c4ccd3",
        "#f05b72",
        "#ef5b9c",
        "#f47920",
        "#905a3d",
        "#fab27b",
        "#2a5caa",
        "#444693",
        "#726930",
        "#b2d235",
        "#6d8346",
        "#ac6767",
        "#1d953f",
        "#6950a1",
        "#918597"
    ],
    "series": [
        {
      
            "type": "map",
            "name": "\u9f50\u9f50\u54c8\u5c14",
            "label": {
      
                "show": true,
                "position": "top",
                "margin": 8
            },
            "mapType": "\u9f50\u9f50\u54c8\u5c14",
            "data": [
                {
      
                    "name": "\u9f50\u9f50\u54c8\u5c14\u5e02",
                    "value": "526.7"
                },
                {
      
                    "name": "\u8bb7\u6cb3\u5e02",
                    "value": "67.76"
                },
                {
      
                    "name": "\u9f99\u6c5f\u53bf",
                    "value": "58.08"
                },
                {
      
                    "name": "\u62dc\u6cc9\u53bf",
                    "value": "55.32"
                },
                {
      
                    "name": "\u4f9d\u5b89\u53bf",
                    "value": "46.35"
                },
                {
      
                    "name": "\u514b\u5c71\u53bf",
                    "value": "45.65"
                },
                {
      
                    "name": "\u7518\u5357\u53bf",
                    "value": "37.55"
                },
                {
      
                    "name": "\u5efa\u534e\u533a",
                    "value": "31.63"
                },
                {
      
                    "name": "\u6cf0\u6765\u53bf",
                    "value": "30.35"
                },
                {
      
                    "name": "\u5bcc\u88d5\u53bf",
                    "value": "28"
                },
                {
      
                    "name": "\u9f99\u6c99\u533a",
                    "value": "27.86"
                },
                {
      
                    "name": "\u514b\u4e1c\u53bf",
                    "value": "27.71"
                },
                {
      
                    "name": "\u94c1\u950b\u533a",
                    "value": "26.63"
                },
                {
      
                    "name": "\u5bcc\u62c9\u5c14\u57fa\u533a",
                    "value": "21.48"
                },
                {
      
                    "name": "\u6885\u91cc\u65af\u8fbe\u65a1\u5c14\u65cf\u533a",
                    "value": "16.4"
                },
                {
      
                    "name": "\u6602\u6602\u6eaa\u533a",
                    "value": "7.21"
                },
                {
      
                    "name": "\u78be\u5b50\u5c71\u533a",
                    "value": "7.03"
                }
            ],
            "roam": true,
            "aspectScale": 0.75,
            "nameProperty": "name",
            "selectedMode": false,
            "zoom": 1,
            "mapValueCalculation": "sum",
            "showLegendSymbol": true,
            "emphasis": {
      }
        }
    ],
    "legend": [
        {
      
            "data": [
                "\u9f50\u9f50\u54c8\u5c14"
            ],
            "selected": {
      
                "\u9f50\u9f50\u54c8\u5c14": true
            },
            "show": true,
            "padding": 5,
            "itemGap": 10,
            "itemWidth": 25,
            "itemHeight": 14
        }
    ],
    "tooltip": {
      
        "show": true,
        "trigger": "item",
        "triggerOn": "mousemove|click",
        "axisPointer": {
      
            "type": "line"
        },
        "showContent": true,
        "alwaysShowContent": false,
        "showDelay": 0,
        "hideDelay": 100,
        "textStyle": {
      
            "fontSize": 14
        },
        "borderWidth": 0,
        "padding": 5
    },
    "title": [
        {
      
            "text": "\u9f50\u9f50\u54c8\u5c14\u5404\u533a\u53bf\u4eba\u53e3\u5206\u5e03",
            "subtext": "\u6570\u636e\u6765\u6e90\uff1a\u4e2d\u56fd\u7edf\u8ba1\u5e74\u9274\uff08\u4e07\u4eba\uff09",
            "right": "center",
            "top": "5%",
            "padding": 5,
            "itemGap": 10
        }
    ],
    "visualMap": {
      
        "show": true,
        "type": "piecewise",
        "min": 0,
        "max": 100,
        "inRange": {
      
            "color": [
                "#50a3ba",
                "#eac763",
                "#d94e5d"
            ]
        },
        "calculable": true,
        "inverse": false,
        "splitNumber": 5,
        "orient": "vertical",
        "showLabel": true,
        "itemWidth": 20,
        "itemHeight": 14,
        "borderWidth": 0,
        "pieces": [
            {
      
                "min": 0,
                "max": 5,
                "label": "0-5\u4e07",
                "color": "#CCFF99"
            },
            {
      
                "min": 6,
                "max": 10,
                "label": "6-10\u4e07",
                "color": "#88AA00"
            },
            {
      
                "min": 11,
                "max": 20,
                "label": "11-20\u4e07",
                "color": "#886600"
            },
            {
      
                "min": 21,
                "max": 50,
                "label": "21-50\u4e07",
                "color": "#CC6600"
            },
            {
      
                "min": 51,
                "max": 100,
                "label": "51-100\u4e07",
                "color": "#CC0000"
            }
        ]
    }
};
        chart_1251ae6f601149799aa7fbde2f6c0659.setOption(option_1251ae6f601149799aa7fbde2f6c0659);
    script>
body>
html>

你可能感兴趣的:(python,python)