# # 導入包
# from pyecharts.charts import Bar, Timeline
# from pyecharts.options import LabelOpts, TitleOpts
# from pyecharts.globals import ThemeType
#
# # 從文件中讀取信息
# GDP_file = open("1960-2019全球GDP數據.csv", "r", encoding="GB2312")
# GDP_data = GDP_file.readlines()
#
# # 關閉文件
# GDP_file.close()
#
# # 規整文件
# GDP_data.pop(0)
#
# # 定義一個字典來存文件
# GDP_dict = {}
# for line_data in GDP_data:
# GDP_year = int(line_data.split(",")[0])
# GDP_country = line_data.split(",")[1]
# GDP = float(line_data.split(",")[2])
# try:
# GDP_dict[GDP_year].append([GDP_country, GDP])
# except KeyError:
# GDP_dict[GDP_year] = []
# GDP_dict[GDP_year].append([GDP_country, GDP])
#
# # 創建時間線(并且設置其主題)
# GDP_line = Timeline({"theme": ThemeType.ROMA})
#
# # 排序數據對象
# sort_years = sorted(GDP_dict.keys())
#
# for year in GDP_dict:
# GDP_dict[year].sort(key=lambda element: element[1], reverse=True)
# year_data = GDP_dict[year][:8:]
# x_data = []
# y_data = []
# # 為x,y準備數據
# for country_data in year_data:
# x_data.append(country_data[0])
# y_data.append(country_data[1] / 100000000)
# # 建立柱狀圖
# GDP_bar = Bar()
# x_data.reverse()
# y_data.reverse()
# GDP_bar.add_xaxis(x_data)
# GDP_bar.add_yaxis("GDP(億)", y_data, label_opts=LabelOpts(position="right"))
#
# # 反轉x-y軸
# GDP_bar.reversal_axis()
#
# # 設置每一年的標題
# GDP_bar.set_global_opts(
# title_opts=TitleOpts(title=f"{year}年全球前八國家的GDP")
# )
#
# # 創建時間線
# GDP_line.add(GDP_bar, str(year))
#
# # 調整時間軸播放
# GDP_line.add_schema(
# play_interval=3000, # 時間移動的時間
# is_timeline_show=True, # 展示時間線
# is_auto_play=True, # 自動播放
# is_loop_play=True # 循環播放
# )
#
# # 生成柱狀圖
# GDP_line.render("1960-2019年全球GDP top8變化圖(new).html")# 導入包
from pyecharts.charts import Bar, Timeline
from pyecharts.options import LabelOpts, TitleOpts
from pyecharts.globals import ThemeType# 從文件中得到GDP數據
GDP_file = open("1960-2019全球GDP數據.csv", "r", encoding="GB2312")
GDP_data = GDP_file.readlines()# 關閉文件
GDP_file.close()# 規整數據
GDP_data.pop(0)# 創建GDP字典,便于讀數據
GDP_dict = {}# 為字典讀數據
for line in GDP_data:year = int(line.split(",")[0]) # 得到年份數據country = line.split(",")[1] # 得到國家數據GDP = float(line.split(",")[2]) # 得到GDP數據# 每一年的第一個數據進入字典的時候是沒有列表的,所以說要先try一下try:GDP_dict[year].append([country, GDP]) # 假如列表已經存在,則可以直接appendexcept KeyError:GDP_dict[year] = [] # 假如列表不存在,則先創造再添加GDP_dict[year].append([country, GDP])# 排序年份
sort_year = sorted(GDP_dict.keys())# 創建時間線(并且對其進行初始設置)
GDP_timeline = Timeline({"theme": ThemeType.CHALK})# 準備創造柱狀圖
for year in GDP_dict:GDP_dict[year].sort(key=lambda element: element[1], reverse=True)year_data = GDP_dict[year][:8:]x_data = []y_data = []for country in year_data:x_data.append(country[0])y_data.append(country[1] / 100000000)# 創建柱狀圖GDP_bar = Bar()x_data.reverse()y_data.reverse()GDP_bar.add_xaxis(x_data)GDP_bar.add_yaxis("GDP(億)", y_data, label_opts=LabelOpts(position="right"))# 反轉x-y軸GDP_bar.reversal_axis()# 設置每年的標題GDP_bar.set_global_opts(title_opts=TitleOpts(title=f"{year}年全球GDP排名前八國家"))# 時間線增加GDP_timeline.add(GDP_bar, str(year))# 設置時間線
GDP_timeline.add_schema(play_interval=3000, # 時間移動的時間is_timeline_show=True, # 展示時間線is_auto_play=True, # 自動播放is_loop_play=True # 循環播放
)# 生成柱狀圖
GDP_timeline.render("1960-2019年全球GDP top8變化圖(mine).html")