coding:utf-8

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

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

minNum=180 maxNum=100000 def get_gdp_map2(datas): min_data, max_data = (minNum, maxNum) tl = Timeline(init_opts=opts.InitOpts(renderer='svg', width='100%', height='650px', bg_color='black')) # 设置画布背景颜色为黑色 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)
    year_data = year_data.sort_values(by=str(i) + '年', ascending=True)

    # Create the bar chart for the current year
    bar_chart = (
        Bar()
        .add_xaxis(year_data['地区'].tolist())
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .add_yaxis(''.format(i), year_data[str(i) + '年'].tolist(), category_gap=0)

        .reversal_axis()
        .set_global_opts(
            title_opts=opts.TitleOpts(title='{}年GDP排名前十的省份'.format(i), pos_top='60%', pos_left='10%'),
            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),pos_left='center',  # 标题居中显示
                        pos_top='5%',    title_textstyle_opts=opts.TextStyleOpts(font_size=25)),   # 标题
            xaxis_opts=opts.AxisOpts(
                max_=maxNum,  axislabel_opts=opts.LabelOpts(is_show=False)
            ),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
            tooltip_opts=opts.TooltipOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(
                is_calculable=True,
                dimension=0,
                pos_right='20%',
                pos_top='60%',
                range_text=['High', 'Low'],
                range_color=['lightskyblue', 'yellow', 'orangered'],
                textstyle_opts=opts.TextStyleOpts(color='#ddd'),
                min_=min_data,
                max_=max_data

            ),
            # legend_opts=opts.LegendOpts(pos_left='80%'),  # 图例向右移动
        )
    )

    grid_chart = (
        Grid()
        .add(bar_chart, grid_opts=opts.GridOpts(pos_left='10%', pos_bottom='10%', pos_right='50%', height='25%'))
        .add(map_chart, grid_opts=opts.GridOpts(pos_bottom='60%', height='40%'))
    )

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

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

get_gdp_map2(datas) 怎样减画布背景换成黑色? 在上述代码中,通过在初始化选项(InitOpts)中设置bg_color='black',将画布背景颜色设置为黑色。

Pyecharts 地图和柱状图:2001-2019年中国各省GDP数据可视化

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

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