[pyecharts library and pandas library] Use the pyecharts library to draw the GDP heat map of China's provinces in 2021 and the histogram of the per capita GDP and population of the five provinces with lower GDP based on the GDP data in the table

Program to solve problem description:

Create the China GDP.csv file according to the chart below, then open the file to read the 2021 GDP data, display the GDP heat map of each province (excluding Taiwan Province) on the map of China, and display the population and per capita GDP of the five provinces with the lowest GDP in a histogram Relationship.

A screenshot of the data table is as follows:

The data comes from the Internet for reference only

When using the program for data processing, it is necessary to save the above data as a csv file.

 The program code is as follows:

from pyecharts import options as opts
from pyecharts.charts import Map
import pandas as pd
from pyecharts.charts import Bar,Page

file_name = '中国各省份GDP(1).csv'
reader = pd.read_csv(file_name, encoding='gbk')
prvcnm = reader['省份']
gdp = reader['2021'] # 读取时为字符串,转为float类型数据
zhsf=list(reader['省份'].tail(5))
zhrk=list(reader['人口'].tail(5))
zhrjgdp=list(reader['人均'].tail(5))
prvc_gdp_growth = list(zip(prvcnm, gdp))
page=Page(layout=Page.DraggablePageLayout)
customMap = (
    Map()
        .add("GDP",  # 图例
             prvc_gdp_growth,  # 数据项
             "china", 
             is_map_symbol_show=False,  #
             )

        .set_series_opts(
        label_opts=opts.LabelOpts(  # 设置标签配置项
            is_show=True  
        )
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(  # 设置标题配置项
            title="2021年中国各省GDP增长地理分布图",  # 设置标题名称
            pos_left="center" , # 设置标题居中
        ),
        # 设置图例配置项
        legend_opts=opts.LegendOpts(
            pos_right="left",  # 设置为水平居左
            pos_bottom="bottom"  # 设置为垂直居下
        ),

        # 设置视觉映射配置项
        visualmap_opts=opts.VisualMapOpts(max_=90000,
                                          min_=10000,
                                          is_piecewise=True,
                                          pieces=[{"max": 90000, "min": 52000, "label": ">=52000", "color": "#B40404"},
                                                  {"max": 52000, "min": 32000, "label": "32000-52000", "color": "#DF0101"},
                                                 {"max": 32000, "min": 16000, "label": "16000-32000", "color": "#F78181"},
                                                  {"max": 16000, "min": 8000, "label": "8000-16000", "color": "#F5A9A9"},
                                                  {"max": 8000, "min": 1000, "label": "1000-8000", "color": "#FFFFCC"}]

        )
    )
)
bar=(
    Bar()
    .set_global_opts(
        title_opts=opts.TitleOpts(  
            title="2021年GDP最低的五个省人口和人均GDP",  # 设置标题名称
            pos_left="center",  # 设置标题居中
        ),
        legend_opts=opts.LegendOpts(
            pos_right="right",
            pos_left="left",# 设置为水平居左
            pos_top="top"  # 设置为垂直居下
        ),
        yaxis_opts=opts.AxisOpts(
            name='人口/万人',
            offset=10
        )
    )
)
bar.add_xaxis(zhsf) # 添加x轴数据
bar.add_yaxis('人口/万人',zhrk,yaxis_index=0) # 添加y轴数据

bar.add_yaxis('人均GDP',zhrjgdp,yaxis_index=1)
bar.extend_axis(
    yaxis=(opts.AxisOpts(
        name="人均GDP",
        min_=0,
        max_=10,
    )
    )
)
page.add(customMap,bar)
page.render("demo33.html") # 显示图表

 The result of the program running is as follows:

The running results of the program are saved in the webpage and can be opened with a browser.

 

GDP data and operating results are only used for learning and communication exercises, and the maps used are only used for learning Python programming, and non-standard Chinese maps.

Follow bloggers to learn more about python programming knowledge! 

Guess you like

Origin blog.csdn.net/qq_59049513/article/details/122744605