学习中弄得时间轴柱状图,GDP变化

本文展示了如何使用Python库Pyecharts生成一个动态的Timeline图表,展示了1960年至2019年全球前八个国家的GDP数据,按年份排序并以递减顺序显示。
摘要由CSDN通过智能技术生成
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
from pyecharts.options import TitleOpts,ToolboxOpts
f = open("D:\\1960-2019全球GDP数据.csv","r",encoding="GB2312")
data_lines = f.readlines()
f.close()
data_lines.pop(0)
data_dict = {}
for line in data_lines:
    year = int(line.split(",")[0])
    country = line.split(",")[1]
    gdp = float(line.split(",")[2])
    try:
        data_dict[year].append([country,gdp])
    except KeyError:
        data_dict[year] = []
        data_dict[year].append([country,gdp])
timeline = Timeline({"theme":ThemeType.LIGHT})
sorted_year_list = sorted(data_dict.keys())
for year in sorted_year_list:
    data_dict[year].sort(key=lambda element:element[1],reverse=True)
    year_data = data_dict[year][0:8][::-1]
    x_data = []
    y_data = []
    for country_gdp in year_data:
        x_data.append(country_gdp[0])
        y_data.append(country_gdp[1]/100000000)
        bar = Bar()
        bar.add_xaxis(x_data)
        bar.add_yaxis("GDP(亿)",y_data,label_opts=LabelOpts(position="right"))
        bar.reversal_axis()
        bar.set_global_opts(
            title_opts=TitleOpts(title=f"{year}年前八GDP数据"),
            toolbox_opts=ToolboxOpts(is_show=True),
        )
        timeline.add(bar,str(year))
timeline.add_schema(
    play_interval=300,
    is_timeline_show=True,
    is_auto_play=True,
    is_loop_play=False
)

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值