基础柱状图构建
通过Bar构建基础柱状图
1 2 3 4 5 6 7 8 9 10
| from pyecharts.charts import Bar from pyecharts.options import *
bar = Bar()
bar.add_xaxis(["中国", "美国", "英国"])
bar.add_yaxis("GDP", [30, 20, 10])
bar.render("基础柱状图.html")
|
反转x和y轴
1 2 3 4 5 6 7 8 9 10
| bar = Bar()
bar.add_xaxis(["中国", "美国", "英国"])
bar.add_yaxis("GDP", [30, 20, 10])
bar.reversal_axis()
bar.render("基础柱状图.html")
|
数值标签在右侧
1 2 3 4 5 6 7 8 9 10
| bar = Bar()
bar.add_xaxis(["中国", "美国", "英国"])
bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right"))
bar.reversal_axis()
bar.render("基础柱状图.html")
|
总结
- 通过Bar()创建一个柱状图对象
- 和折线图一样,通过
add_xaxis()
和add_yaxis()
添加x和y轴数据
- 通过柱状图对象的:
reversal_axis()
,反转x和y轴
- 通过
label_opts=LabelOpts(position="right")
设置数值标签在右侧显示
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| """ 演示基础柱状图的开发 """ from pyecharts.charts import Bar from pyecharts.options import LabelOpts
bar = Bar()
bar.add_xaxis(["中国", "美国", "英国"])
bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right"))
bar.reversal_axis()
bar.render("基础柱状图.html")
|
基础时间线柱状图绘制
创建时间线
Timeline()
-时间线
柱状图描述的是分类数据,回答的是每一个分类中“有多少?”这个问题。这是柱状图的主要特点,同时柱状图很难动态的描述一个趋势性的数据,这里pyecharts
为我们提供了一种解决方案-时间线
如果说一个Bar、Line对象是一张图表的话,时间线就是创建一个一维的x轴,轴上每一个点就是一个图表对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from pyecharts.charts import Bar, Timeline from pyecharts.options import * bar1 = Bar() bar1.add_xaxis(["中国", "美国", "英国"]) bar1.add_yaxis("GDP", [30, 30, 20], 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.render("基础柱状图-时间线.html")
|
自动播放
1 2 3 4 5 6 7
| timeline.add_schema( play_interval=1000, is_timeline_show=True, is_auto_play=True, is_loop_play=True )
|
时间线设置主题
1 2 3 4 5
| from pyecharts.globals import ThemeType
timeline = Timeline( {"theme": ThemeType.LIGHT} )
|
编号 |
颜色 |
备注 |
ThemeType.WHITE |
红蓝 |
默认颜色等同于bar = Bar() |
ThemeType.LIGHT |
蓝黄粉 |
高亮颜色 |
ThemeType.DARK |
红蓝 |
黑色背景 |
ThemeType.CHALIK |
红蓝 绿 |
黑色背景 |
ThemeType.ESSOS |
红黄 |
暖色系 |
ThemeType.INFOGRAPHIC |
红蓝黄 |
偏亮 |
ThemeType.MACARONS |
紫绿 |
|
ThemeType.PURPLE_PASSION |
粉紫 |
灰色背景 |
ThemeType.ROMA |
红黑灰 |
偏暗 |
ThemeType.ROMANTIC |
红粉蓝 |
淡黄色背景 |
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| """ 演示带有时间线的柱状图开发 """ from pyecharts.charts import Bar, Timeline from pyecharts.options import LabelOpts from pyecharts.globals import ThemeType bar1 = Bar() bar1.add_xaxis(["中国", "美国", "英国"]) bar1.add_yaxis("GDP", [30, 30, 20], label_opts=LabelOpts(position="right")) bar1.reversal_axis() bar2 = Bar() bar2.add_xaxis(["中国", "美国", "英国"]) bar2.add_yaxis("GDP", [50, 50, 50], label_opts=LabelOpts(position="right")) bar2.reversal_axis() bar3 = Bar() bar3.add_xaxis(["中国", "美国", "英国"]) bar3.add_yaxis("GDP", [70, 60, 60], label_opts=LabelOpts(position="right")) bar3.reversal_axis()
timeline = Timeline({"theme": ThemeType.LIGHT})
timeline.add(bar1, "点1") timeline.add(bar2, "点2") timeline.add(bar3, "点3")
timeline.add_schema( play_interval=1000, is_timeline_show=True, is_auto_play=True, is_loop_play=True )
timeline.render("基础时间线柱状图.html")
|
动态GDP柱状图绘制
列表的sort方法
在前面我们学习过sorted函数,可以对数据容器进行排序
在后面的数据处理中,我们需要对列表进行排序,并指定排序规则,sorted函数就无法完成了
我们补充学习列表的sort方法
使用方式:
列表.sort(key=选择排序依据的函数,reverse=True|False)
- 参数key,是要求传入一个函数,表示将列表的每一个元素都传入函数中,返回排序的依据
- 参数reverse,是否反转排序结果,True表示降序,False表示升序
带名函数形式
1 2 3 4 5 6 7 8
|
my_list = [["a", 33], ["b", 55], ["c", 11]]
def choose_sort_key(element): return element[1] my_list.sort(key=choose_sort_key, reverse=True) print(my_list)
|
匿名lambda模式
1 2 3 4 5
|
my_list = [["a", 33], ["b", 55], ["c", 11]] my_list.sort(key=lambda element: element[1], reverse=True) print(my_list)
|
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| """ 扩展列表的sort方法 在学习了将函数作为参数传递后,我们可以学习列表的sort方法来对列表进行自定义排序 """
my_list = [["a", 33], ["b", 55], ["c", 11]]
def choose_sort_key(element): return element[1] my_list.sort(key=choose_sort_key, reverse=True) print(my_list)
my_list.sort(key=lambda element: element[1], reverse=True) print(my_list)
|
运行结果
1 2
| [['b', 55], ['a', 33], ['c', 11]] [['b', 55], ['a', 33], ['c', 11]]
|
数据处理
需求分析
- GDP数据处理为亿级
- 有时间轴,按照年份为时间轴的点
- x轴和y轴反转,同时每一年的数据只要前8名国家
- 有标题,标题的年份会动态更改
- 设置了主题为LIGHT
处理数据
将数据转换为字典存储,格式为:
1
| {年份:[[国家, gdp], [国家, gdp], ......], 年份:[[国家, gdp],[国家, gdp],......], ......}
|
1 2 3 4 5 6 7 8 9 10 11
| data_dict = {} for line in 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))
|
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| """ 演示第三个图表:GDP动态柱状图开发 """ from pyecharts.charts import Bar, Timeline from pyecharts.options import * from pyecharts.globals import ThemeType
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=True )
timeline.render("1960-2019全球GDP前8国家.html")
|