程序解决问题描述:
根据下图表建立中国GDP.csv文件,然后打开文件读取2021年GDP数据,以中国地图显示各省(未包括台湾省)的GDP热力图,用柱状图显示GDP最低的五个省人口和人均GDP的关系。
数据表格截图如下:
数据源自网络仅供参考
在运用程序进行数据处理时需要将上述数据保存为csv文件。
程序代码如下:
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") # 显示图表
程序运行结果如下:
程序运行结果保存在网页中,用浏览器打开即可。
GDP数据与运行结果仅做学习交流练习使用,使用的地图仅做学习Python程序设计使用,非标准中国地图。
关注博主学习更多python程序设计知识!