终于来到最后一个数据可视化的文章拿啦~~~
在这里学习如何绘制动态柱状图
我先整个活
(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤
答:
Python的Pyecharts软件包。它是一个用于Python数据可视化和图表绘制的库,可用于制作各种图表和可视化结果,包括柱状图、折线图、饼图、散点图、地图等。Pyecharts使用JavaScript的ECharts库进行底层绘制,因此它提供了易于使用的Python界面和大量的示例代码。同时,Pyecharts还支持在Jupyter notebook中的实时交互和动态展示,使其非常适合于数据分析和数据可视化。

(2)反转X,Y轴 

- """
- 构建基础柱状图
- """
- from pyecharts.charts import Bar
- from pyecharts.options import LabelOpts
-
- # 使用Bar构建基础柱状图
- bar = Bar()
- # 添加X轴
- bar.add_xaxis(["中国", "美国", "英国"])
- # 添加Y轴
- # 设置数值标签在右侧
- bar.add_yaxis("GDP",
- [30, 20, 10],
- label_opts=
- LabelOpts(position="right"))
- # 反转x轴和y轴
- bar.reversal_axis()
-
- # 绘图
- bar.render("普通柱状图.html")



- """
- 基础时间柱状图
- """
- 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, 20, 10],
- label_opts=LabelOpts(position="right")
- )
- bar1.reversal_axis()
-
- bar2 = Bar()
- bar2.add_xaxis(['中国', '美国', '英国'])
- bar2.add_yaxis("GDP",
- [50, 50, 100],
- label_opts=LabelOpts(position="right")
- )
- bar2.reversal_axis()
-
- bar3 = Bar()
- bar3.add_xaxis(['中国', '美国', '英国'])
- bar3.add_yaxis("GDP",
- [1000, 600, 300],
- 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")
-
sort()是Python中用于排序列表的内置函数。使用sort()方法可以对列表进行升序排列或降序排列。
sort()函数有两种用法:
-
- my_list = [2, 5, 1, 9, 4]
- my_list.sort()
- print(my_list) # 输出 [1, 2, 4, 5, 9]
-
- my_list = [2, 5, 1, 9, 4]
- my_list.sort(reverse=True)
- print(my_list) # 输出 [9, 5, 4, 2, 1]
另外,之前学习过sorted()函数对列表进行排序,不同的是,sorted()函数不会修改原始列表,而是返回一个新的已排序的列表。
例如:
- my_list = [2, 5, 1, 9, 4]
- sorted_list = sorted(my_list)
- print(my_list) # 输出 [2, 5, 1, 9, 4]
- print(sorted_list) # 输出 [1, 2, 4, 5, 9]
注:sort()和sorted()函数都是在原始列表的基础上进行排序,因此会对原始列表做出修改或返回一个新的已排序的列表。如果不想对原始列表进行修改,可以先对原始列表进行复制再进行排序。



- """
- GDP动态柱状图绘制
- """
- from pyecharts.charts import Bar, Timeline
- from pyecharts.options import *
- from pyecharts.globals import ThemeType
-
- # 读取数据
- f = open("D:\\IOText\\DataDoing\\1960-2019全球GDP数据.csv", "r", encoding="GB2312")
- data_lines = f.readlines()
- # 关闭文件
- f.close()
- # 删除第一条数据
- data_lines.pop(0)
- # 将数据转化为字典存储,格式
- # 年份: [[国家,gdp],[国家,gdp]]
- # 定义字典对象存储
- 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)
- # 取GDP前八的
- year_data_before8 = data_dict[year][0:8]
- x_data = []
- y_data = []
- for country_GDP in year_data_before8:
- # x轴添加国家
- x_data.append(country_GDP[0])
- # y轴添加GDP
- 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"))
- # 翻转xy轴
- bar.reversal_axis()
- # 设置每一年的图标的标题
- bar.set_global_opts(
- title_opts=TitleOpts(title=f"{year}年全球前八GDP")
- )
- # 加入时间线
- 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前八国家.html")
对于刚刚接触编程的同学来说,这个问题肯定是很难得,但是多练习练习就好了,ヾ(◍°∇°◍)ノ゙
拜拜ヾ( ̄▽ ̄)Bye~Bye~