Pyecharts库的学习历程:绘制Grid组合图表和Timeline轮播图

2 min read Page Views

1.原始数据

2005~2021年中国地区人均可支配收入 数据如下所示:

数据下载地址:

https://data.stats.gov.cn/easyquery.htm?cn=E0103

3.python程序

import pandas as pd
from pyecharts.charts import Bar
from pyecharts.charts import Map
from pyecharts.charts import WordCloud
from pyecharts.charts import Grid
from pyecharts.globals import SymbolType
from pyecharts import options as opts
from pyecharts.charts import Timeline

"""
pandas: 1.5.3
pyecharts: 2.0.8
"""

data = pd.read_excel(io='人均可支配收入.xlsx', sheet_name='Sheet1', index_col=0)
x = data.index.tolist()
y = data.values.T.tolist()
year = data.columns.tolist()
y_max = max([max(row) for row in y])
timeline = Timeline(init_opts=opts.InitOpts(width="1500px", height='600px'))

for i in range(len(year)):
    xy = list(zip(x, y[i]))
    xy.sort(key=lambda x: x[1], reverse=False)
    xy_top = xy[-10:]

    bar = Bar()
    bar.add_xaxis(xaxis_data=[i[0] for i in xy])
    bar.add_yaxis(series_name='', y_axis=[i[1] for i in xy])
    bar.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-90)),
                        yaxis_opts=opts.AxisOpts(max_=y_max))
    bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='top', rotate=-90))

    map = Map()
    map.add(year[i] + '地区人均可支配收入', data_pair=xy, maptype='china', is_map_symbol_show=False, aspect_scale=0.65)
    map.set_global_opts(
        title_opts=opts.TitleOpts(title='2005~2021年地区人均可支配收入', subtitle='数据来源:国家统计局\n\n单位:元'),
        visualmap_opts=opts.VisualMapOpts(max_=y_max, is_piecewise=True,
                                          range_color=['lightskyblue', 'yellow', 'orangered']))

    wordcloud = WordCloud()
    wordcloud.add(series_name='', data_pair=xy, word_size_range=[5, 45], shape=SymbolType.DIAMOND,
                  is_draw_out_of_bound=False, width='600px', height='180px', pos_left='10px', pos_top='300px')

    grid = Grid()
    grid.add(bar, grid_opts=opts.GridOpts(pos_left='50px', pos_top='80px', width='500px', height='120px'))
    grid.add(map, grid_opts=opts.GridOpts(height='500px'))
    grid.add(wordcloud, grid_opts=opts.GridOpts(height='180px'))

    timeline.add(grid, year[i]).add_schema(is_auto_play=True, pos_left='10%', pos_right='35%')

timeline.render('人均可支配收入.html')

3.效果展示

Last updated on 2025-05-21