1.基础柱状图
1.1通过Bar构建基础柱状图
from pyecharts.charts import Bar
from pyecharts.options import *
bar = Bar()
bar.add_xaxis(["中国","美国","英国"])
bar.add_yaxis("GDP",[30,20,10])
bar.render("基础柱状图.html")
1.2反转x轴,y轴
from pyecharts.charts import Bar
from pyecharts.options import *
bar = Bar()
bar.add_xaxis(["中国","美国","英国"])
bar.add_yaxis("GDP",[30,20,10])
bar.reversal_axis()
bar.render("基础柱状图.html")
1.3数值标签在右侧
from pyecharts.charts import Bar
from pyecharts.options import *
bar = Bar()
bar.add_xaxis(["中国","美国","英国"])
bar.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar.reversal_axis()
bar.render("基础柱状图.html")
1.4总结
- 通过Bar()构建一个柱状图对象
- 和折线图一样,通过add_xaxis()和add_yaxis()添加x和y轴数据
- 通过柱状图对象的:reversal_axis(),反转x和y轴
- 通过label_opts=LabelOpts(position=“right”)设置数值标签在右侧显示
2.基础时间柱状图
2.1掌握基础的时间线配置动态图表
- Timeline()-时间线
- 如果说一个Bar、Line对象是一张图表的话,时间线就是创建一维的x轴,轴上每一个点就是一个图表对象
2.2创建时间线
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
bar1 = Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis("GDP",[30,20,10])
bar1.reversal_axis()
bar2 = Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,30,20])
bar2.reversal_axis()
timeline = Timeline()
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
timeline.render("基础时间柱状图-时间线.html")
2.3自动播放
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import *
bar1 = Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2 = Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,30,20],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
timeline = Timeline()
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True
)
timeline.render("基础时间柱状图-时间线.html")
2.4时间线设置主题
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import *
bar1 = Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis("GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2 = Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,30,20],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
timeline = Timeline({"theme":ThemeType.DARK})
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True
)
timeline.render("基础时间柱状图-时间线.html")
2.5总结
- 时间线
- 自动播放
- 如何设置主题 timeline = Timeline({“theme”: ThemeType.LIGHT})
3.GDP动态柱状图绘制
3.1掌握列表的sort方法并配合配合lambda匿名函数完成列表排序
如下嵌套列表,要求对外层表进行排序,排序依据是内层表的第二个元素数字
方法1:
my_list = [["a",3],["b",5],["c",1]]
def choose_sort_key(element):
return element[1]
my_list.sort(key=choose_sort_key,reverse=True)
my_list.sort(key=lambda element: element[1],reverse=True)
print(my_list)
[['b', 5], ['a', 3], ['c', 1]]
方法2:配合lmbda函数完成
my_list = [["a",3],["b",5],["c",1]]
my_list.sort(key=lambda element: element[1],reverse=True)
print(my_list)
[['b', 5], ['a', 3], ['c', 1]]
3.2处理数据
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]
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()
x_data.reverse()
y_data.reverse()
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}年全球前8GDP数据")
)
timeline.add(bar,str(year))
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
timeline.render("1960-2019全球GDP前8国家.html")
3.3最终结果