問題を解決するプログラムの説明:
以下の図に従って中国 GDP.csv ファイルを作成し、ファイルを開いて 2021 年の GDP データを読み取り、中国地図上に各省 (台湾省を除く) の GDP ヒート マップを表示し、人口と 1 人当たりを表示します。ヒストグラム関係で GDP が最も低い 5 つの州の 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") # 显示图表
プログラムを実行した結果は次のようになります。
プログラムの実行結果は Web ページに保存され、ブラウザで開くことができます。
GDP データと運用結果は学習とコミュニケーション演習にのみ使用され、使用される地図は Python プログラミングの学習と非標準の中国地図のみに使用されます。
ブロガーをフォローして、Python プログラミングの知識をさらに学びましょう。