| 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") |
| 1. 通过Bar()构建一个柱状图对象 |
| 2. 和折线图一样,通过add_xaxis()和add_yaxis()添加x和y轴数据 |
| 3. 通过柱状图对象的:reversal_axis(),反转x和y轴 |
| 4. 通过label_opts=LabelOpts(position="right")设置数值标签在右侧显示 |
| 如果说一个Bar、Line对象是一张图表的话,时间线就是创建一个一维的x轴,轴上每一个点就是一个图表对象 |



| 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") |
| 列表.sort(key=选择排序依据的函数, reverse=True|False) |
| 参数key,是要求传入一个函数,表示将列表的每一个元素都传入函数中,返回排序的依据 |
| 参数reverse,是否反转排序结果,True表示降序,False表示升序 |
| |
| 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) |
| |
| |
| my_list.sort(key=lambda element: element[1], reverse=True) |
| |
| print(my_list) |
| 1. GDP数据处理为亿级 |
| 2. 有时间轴,按照年份为时间轴的点 |
| 3. x轴和y轴反转,同时每一年的数据只要前8名国家 |
| 4. 有标题,标题的年份会动态更改 |
| 5. 设置了主题为LIGHT |

| 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=False |
| ) |
| |
| timeline.render("1960-2019全球GDP前8国家.html") |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术