coding:utf-8

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

datas = pd.read_csv('2001-2019各省GDP数据.csv', encoding='gbk')

def get_gdp_map2(datas): tl = Timeline() tl.add_schema(play_interval=500, symbol='emptydiamond')

for i in range(2001, 2020):
    # Filter the data for the current year
    year_data = datas[['地区', str(i) + '年']].copy()
    year_data.sort_values(by=str(i) + '年', ascending=False, inplace=True)
    year_data = year_data.head(10)

    # Create the bar chart for the current year
    bar_chart = (
        Bar()
        .add_xaxis(year_data['地区'].tolist())
        .add_yaxis('{}年GDP排名前十'.format(i), year_data[str(i) + '年'].tolist(), category_gap=0)
        .set_global_opts(
            title_opts=opts.TitleOpts(title='{}年GDP排名前十的省份'.format(i)),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45)),
            visualmap_opts=opts.VisualMapOpts(is_show=False),
        )
    )

    # Create the map chart for the current year
    map_chart = (
        Map()
        .add('全国各省GDP(亿元)', datas[['地区', str(i) + '年']].values.tolist(), 'china')
        .set_global_opts(
            title_opts=opts.TitleOpts(title='{}年全国各省GDP(亿元)'.format(i)),
            visualmap_opts=opts.VisualMapOpts(is_show=True, max_=110000, is_piecewise=True),
        )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    )

    # Create the grid chart for the current year
    grid_chart = (
        Grid()
        .add(bar_chart, grid_opts=opts.GridOpts(pos_left='10%', pos_bottom='50%', pos_right='10%', height='40%'))
        .add(map_chart, grid_opts=opts.GridOpts(height='60%'))
        .set_global_options(height='100%')
    )

    tl.add(grid_chart, '{}年'.format(i))

tl.render('2001-2019年全国各省GDP.html')

get_gdp_map2(datas)

在代码中,使用了 Grid 来将地图和柱状图放在同一个画布上。然而,set_global_opts() 方法中没有 grid_opts 参数,因此会报错。要解决这个问题,可以将 set_global_opts() 方法改为 set_global_options() 方法。

此外,想要将画布调整为全界面,可以使用 Grid 的 height 参数设置画布的高度为 100%。

修改后的代码如下:

# coding:utf-8
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Map, Timeline
import pandas as pd

datas = pd.read_csv('2001-2019各省GDP数据.csv', encoding='gbk')


def get_gdp_map2(datas):
    tl = Timeline()
    tl.add_schema(play_interval=500, symbol='emptydiamond')

    for i in range(2001, 2020):
        # Filter the data for the current year
        year_data = datas[['地区', str(i) + '年']].copy()
        year_data.sort_values(by=str(i) + '年', ascending=False, inplace=True)
        year_data = year_data.head(10)

        # Create the bar chart for the current year
        bar_chart = (
            Bar()
            .add_xaxis(year_data['地区'].tolist())
            .add_yaxis('{}年GDP排名前十'.format(i), year_data[str(i) + '年'].tolist(), category_gap=0)
            .set_global_opts(
                title_opts=opts.TitleOpts(title='{}年GDP排名前十的省份'),
                xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45)),
                visualmap_opts=opts.VisualMapOpts(is_show=False),
            )
        )

        # Create the map chart for the current year
        map_chart = (
            Map()
            .add('全国各省GDP(亿元)', datas[['地区', str(i) + '年']].values.tolist(), 'china')
            .set_global_opts(
                title_opts=opts.TitleOpts(title='{}年全国各省GDP(亿元)'),
                visualmap_opts=opts.VisualMapOpts(is_show=True, max_=110000, is_piecewise=True),
            )
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        )

        # Create the grid chart for the current year
        grid_chart = (
            Grid()
            .add(bar_chart, grid_opts=opts.GridOpts(pos_left='10%', pos_bottom='50%', pos_right='10%', height='40%'))
            .add(map_chart, grid_opts=opts.GridOpts(height='60%'))
            .set_global_options(height='100%')
        )

        tl.add(grid_chart, '{}年'.format(i))

    tl.render('2001-2019年全国各省GDP.html')


get_gdp_map2(datas)

这样修改后,map 地图和 bar 图就会在画布上分开显示,并且画布会调整为全界面。

Pyecharts 地图和柱状图重叠问题解决方法:使用 Grid 组件

原文地址: https://www.cveoy.top/t/topic/eQqX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录