python数据可视化,列表的sort方法 您所在的位置:网站首页 python在线播放 python数据可视化,列表的sort方法

python数据可视化,列表的sort方法

2023-05-19 03:01| 来源: 网络整理| 查看: 265

from pyecharts.charts import Map from pyecharts.options import VisualMapOpts # 构建地图对象 map = Map() # 准备数据 data = [ ("北京市", 99), # 列表嵌套元组 ("上海市", 199), ("湖南省", 299), ("台湾省", 199), ("安徽省", 299), ("广东省", 399), ("湖北省", 599) ] # 添加数据 map.add("地图", data, "china") # 设置全局选项 map.set_global_opts( visualmap_opts=VisualMapOpts( is_show=True, # 是否显示 is_piecewise=True, # 是否分标 pieces=[ {"min": 1, "max": 99, "label": "1-99", "color": "#CCFFFF"}, {"min": 100, "max": 299, "label": "100-299", "color": "#FF6666"}, {"min": 300, "max": 699, "label": "300-699", "color": "#990033"} ] ) ) # 绘图 map.render()

上述代码实现的是全国疫情状况地图

下列代码是柱状图的构建

# 构建柱状图 from pyecharts.charts import Bar from pyecharts.options import LabelOpts # 创建柱状图对象 bar = Bar() # 增加x,y轴 bar.add_xaxis(["中国", "美国", "英国"]) bar.add_yaxis("GDP", ["10", "20", "30"], label_opts=LabelOpts(position="right")) # 将y轴标签显示到右侧 # 反转x轴y轴 bar.reversal_axis() # 显示图像 bar.render()

动态时间轴:

# 构建柱状图 from pyecharts.charts import Bar, Timeline from pyecharts.options import LabelOpts from pyecharts.globals import ThemeType # 创建柱状图对象 bar1 = Bar() bar2 = Bar() bar3 = Bar() # 增加x,y轴,反转x轴y轴 bar1.add_xaxis(["中国", "美国", "英国"]) bar1.add_yaxis("GDP", ["10", "20", "30"], label_opts=LabelOpts(position="right")) # 将y轴标签显示到右侧 bar1.reversal_axis() bar2.add_xaxis(["中国", "美国", "英国"]) bar2.add_yaxis("GDP", ["30", "10", "30"], label_opts=LabelOpts(position="right")) # 将y轴标签显示到右侧 bar2.reversal_axis() bar3.add_xaxis(["中国", "美国", "英国"]) bar3.add_yaxis("GDP", ["50", "30", "10"], label_opts=LabelOpts(position="right")) # 将y轴标签显示到右侧 bar3.reversal_axis() # 创建timeline对象,并设置主题样式 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()

列表的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) # sort方法会把里面的元素传给key对应的函数 # 排序 基于lambda方式 my_list.sort(key=lambda element:element[1], reverse=True) print(my_list)

动态柱状图 :

# GDP动态柱状图 from pyecharts.charts import Bar, Timeline from pyecharts.options import * from pyecharts.globals import * my_file = open("E:/信息.csv", "r", encoding="GB2312") # 获取文件 data_lines = my_file.readlines() # 按行读取文件,返回是一个列表,每行是一个元素 # 关闭文件 my_file.close() # 删除第一行无用数据 data_lines.pop(0) # 将数据转化为字典存储 my_dict = {} for line in data_lines: year = int(line.split(",")[0]) # split返回分割后的字符串列表。 country = line.split(",")[1] gdp = float(line.split(",")[2]) # 当没有key和有key时不同,没有key时会报错 try: my_dict[year].append([country, gdp]) # 有key时追加 except KeyError: my_dict[year] = [] # 无key时先新建一个列表 my_dict[year].append([country, gdp]) # 创建时间线对象 time_line = Timeline({"theme": ThemeType.LIGHT}) # 对年份进行排序 sorted_year_list = sorted(my_dict.keys()) for year in sorted_year_list: my_dict[year].sort(key=lambda element: element[1], reverse=True) # 每年里的数据按照GDP进行排序 year_data = my_dict[year][0:8] # 取一年里的前8个数据 x_data = [] y_data = [] for country_gdp in year_data: # 按照八个数据的来添加x轴和y轴的数据 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}年全球前八GDP") ) time_line.add(bar, str(year)) # 设置自动播放 time_line.add_schema( play_interval=1000, is_timeline_show=True, is_auto_play=True, is_loop_play=False ) # 绘图 time_line.render("1960-2018全国GDP前8数据.html") # 生成的文件名



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有