【pyecharts库与pandas库】利用pyecharts库根据表格中的GDP数据绘制2021年中国各省GDP热力图和GDP较低的五个省的人均GDP与人口的柱状图

程序解决问题描述:

根据下图表建立中国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程序设计知识! 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是使用Python第三方Requests,BeautifulSoup,pandas,Matplotlib,pyecharts分析与统计2022的全国各省GDP分布情况并数据使数据可视化的代码: ```python import requests from bs4 import BeautifulSoup import pandas as pd import matplotlib.pyplot as plt from pyecharts.charts import Map, Geo from pyecharts import options as opts # 获取数据 url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2022/index.html' response = requests.get(url) response.encoding = 'utf-8' soup = BeautifulSoup(response.text, 'html.parser') table = soup.find_all('table')[0] df = pd.read_html(str(table), header=0)[0] df = df[['行政区划代码', '名称', '地区生产总值(亿元)']] df.columns = ['code', 'province', 'gdp'] df['code'] = df['code'].apply(lambda x: x[:2]) df = df.groupby(['code', 'province']).sum().reset_index() # 绘制地图 map_data = [(row['province'], row['gdp']) for _, row in df.iterrows()] map_chart = Map() map_chart.add('2022全国各省GDP分布情况', map_data, 'china') map_chart.set_global_opts( visualmap_opts=opts.VisualMapOpts(max_=max(df['gdp']), is_piecewise=True), title_opts=opts.TitleOpts(title='2022全国各省GDP分布情况'), ) map_chart.render('map.html') # 绘制地理图 geo_data = [(row['province'], row['gdp']) for _, row in df.iterrows()] geo_chart = Geo() geo_chart.add_schema(maptype='china') geo_chart.add('2022全国各省GDP分布情况', geo_data) geo_chart.set_global_opts( visualmap_opts=opts.VisualMapOpts(max_=max(df['gdp']), is_piecewise=True), title_opts=opts.TitleOpts(title='2022全国各省GDP分布情况'), ) geo_chart.render('geo.html') # 绘制柱状图 bar_data = df.sort_values(by='gdp', ascending=False).head(10) plt.bar(bar_data['province'], bar_data['gdp']) plt.xticks(rotation=45) plt.title('2022全国各省GDP排名前十') plt.savefig('bar.png') # 绘制折线图 line_data = df.groupby('code').sum().reset_index().sort_values(by='gdp', ascending=False) plt.plot(line_data['code'], line_data['gdp']) plt.xticks(rotation=45) plt.title('2022全国各省GDP折线图') plt.savefig('line.png') ``` 这段代码会爬取国家统计局的官网,获取2022全国各省GDP数据,并使用pyecharts绘制地图和地理图,使用Matplotlib绘制柱状图和折线图。最终结果会生成4个文件:map.html、geo.html、bar.png和line.png。您可以在浏览器打开map.html和geo.html来查看地图和地理图,也可以在本地查看生成的图片bar.png和line.png。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海岛码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值