import json
from pyecharts.charts import Bar,Timeline
from pyechasts.options import *
from pyecharts.globals import ThemeType
f = open("D:/data.csv","r",encoding="GB2312")
data_lines = f.readlines()
f.close()
data_lines.pop(0)
data_dict = {}
for i in data_lines:
year = int(i.split(",")[0])
country = i.split(",")[1]
GDP = float(i.split(",")[2])
try:
data_dict[year].append([country,GDP])
except KeyError:
data_dict[year] = []
data_dict[year].append([country,GDP])
def sort_gdp(element):
return element[1]
timeline = Timeline(
{"theme":ThemeType.LIGHT}
)
sort_year_list = sorted([data_dict.keys()])
for year in sort_year_list:
data_dict[year].sort(key=sort_gdp,reverse=True)
year_data = data_dict[year][0:8]
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()
timeline.add(bar,str(year))
timeline.add_schema(
play_interval=2000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
效果图