赞
踩
**
**
# 导包 from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts,VisualMapOpts # 创建折线图对象 line = Line() # 给折线图对象添加X轴数据 line.add_xaxis(["中国", "美国", "英国"]) # 给涉嫌图对象添加y轴数据 line.add_yaxis("GDP", [30, 20, 10]) # 全局配置项 line.set_global_opts( title_opts=TitleOpts("GDP展示", pos_left="center", pos_bottom="1%"), legend_opts=LegendOpts(is_show=True), toolbox_opts=ToolboxOpts(is_show=True), visualmap_opts=VisualMapOpts(is_show=True) ) # 使用render方法,将代码生成图像 line.render()
运行结果:
pyecharts官方地址
https://pyecharts.org/#/zh-cn/global_options
地图
import json from pyecharts.charts import * from pyecharts.options import * # 读取疫情数据 f = open("D:/疫情.txt", "r", encoding="UTF-8") data = f.read() # 关闭文件 f.close() # json 转python字典 data_dict = json.loads(data) province_data_list = data_dict["areaTree"][0]["children"] # 组装每个省份的数据和确诊人数为元组,并各个省的数据都封装入列表内 data_list = [] for province_data in province_data_list: province_name = province_data["name"] # 省份名称 province_confirm = province_data["total"]["confirm"] # 确诊人数 data_list.append((province_name, province_confirm)) print(data_list) # 创建地图对象 map = Map() # 添加地图数据 map.add("各省份确诊人数", data_list, "china") # 设置地图全局选项 map.set_global_opts( title_opts=TitleOpts("全国确认地图"), visualmap_opts=VisualMapOpts( is_show=True, # 是否显示 is_piecewise=True, # 是否分段 pieces=[ {"min": 1, "max": 99, "lable": "1-99人", "color": "#CCFFFF"}, {"min": 100, "max": 999, "lable": "100-999人", "color": "#FFFF99"}, {"min": 1000, "max": 4999, "lable": "1000-4999人", "color": "#FF9966"}, {"min": 5000, "max": 9999, "lable": "5000-99999人", "color": "#FF6666"}, {"min": 10000, "max": 99999, "lable": "10000-99999人", "color": "#CC3333"}, {"min": 100000, "lable": "100000+", "color": "#990033"}, ] ) ) map.render("全国疫情地图.html")
运行如果:
网盘资料链接:https://pan.baidu.com/s/1KCKkV13ymltq9CMXE3qxJA?pwd=1234
提取码 :1234
自行下载所需数据
省级地图:
import json from pyecharts.charts import * from pyecharts.options import * # 读取疫情数据 f = open("D:/疫情.txt", "r", encoding="UTF-8") data = f.read() # 关闭文件 f.close() # json 转python字典 data_dict = json.loads(data) cities_data_list = data_dict["areaTree"][0]["children"][3]["children"] # 组装每个市份的数据和确诊人数为元组,并各个省的数据都封装入列表内 data_list = [] for cities_data in cities_data_list: if cities_data["name"] != "境外输入": cities_name = cities_data["name"] + "市" # 城市名称 cities_confirm = cities_data["total"]["confirm"] # 确诊人数 data_list.append((cities_name, cities_confirm)) else: cities_name = cities_data["name"] # 城市名称 cities_confirm = cities_data["total"]["confirm"] # 确诊人数 data_list.append((cities_name, cities_confirm)) data_list.append(("济源市", 5)) print(data_list) # 创建地图对象 map = Map() # 添加地图数据 map.add("河南确诊人数", data_list, "河南") # 设置地图全局选项 map.set_global_opts( title_opts=TitleOpts("河南省确诊地图"), visualmap_opts=VisualMapOpts( is_show=True, # 是否显示 is_piecewise=True, # 是否分段 pieces=[ {"min": 1, "max": 99, "lable": "1-99人", "color": "#CCFFFF"}, {"min": 100, "max": 999, "lable": "100-999人", "color": "#FFFF99"}, {"min": 1000, "max": 4999, "lable": "1000-4999人", "color": "#FF9966"}, {"min": 5000, "max": 9999, "lable": "5000-99999人", "color": "#FF6666"}, {"min": 10000, "max": 99999, "lable": "10000-99999人", "color": "#CC3333"}, {"min": 100000, "lable": "100000+", "color": "#990033"}, ] ) ) map.render("河南疫情地图.html")
运行结果:
柱状图
# 动态柱状图
from pyecharts.charts import *
from pyecharts.options import *
# 使用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 * from pyecharts.options import * from pyecharts.globals import * # 使用bar 构建基础柱状图 bar = Bar() bar.add_xaxis(["中国", "美国", "英国"]) # 添加x轴的数据 bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position="right")) # 添加Y轴数据 bar.reversal_axis() # 反转x和y轴 bar1 = Bar() bar1.add_xaxis(["中国", "美国", "英国"]) # 添加x轴的数据 bar1.add_yaxis("GDP", [40, 30, 20], label_opts=LabelOpts(position="right")) # 添加Y轴数据 bar1.reversal_axis() # 反转x和y轴 bar2 = Bar() bar2.add_xaxis(["中国", "美国", "英国"]) # 添加x轴的数据 bar2.add_yaxis("GDP", [50, 40, 30], label_opts=LabelOpts(position="right")) # 添加Y轴数据 bar2.reversal_axis() # 反转x和y轴 # 构建时间线对象 timeline = Timeline( {"theme": ThemeType.LIGHT}# 设置主题 ) # 在时间线内部添加柱状图对象 timeline.add(bar, "1") timeline.add(bar1, "2") timeline.add(bar2, "3") # 自动播放速度 timeline.add_schema( play_interval=1000, is_timeline_show=True, is_auto_play=True, is_loop_play=True ) # 绘图使用时间线对象绘制图,不是用bar对象 timeline.render("时间线柱状图.html")
运行结果:
动态时间线柱状图
from pyecharts.charts import * from pyecharts.options import * from pyecharts.globals import * # 读取数据 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]) # gdp数据 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: # 针对gdp 排序 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]) # x轴国家 y_data.append(country_gdp[1] / 100000000) # y轴GDP # 构建柱状图对象 bar = Bar() x_data.reverse() y_data.reverse() bar.add_xaxis(x_data) bar.add_yaxis("GDP()亿", y_data, label_opts=LabelOpts(position="reght")) bar.reversal_axis() # 反转x和y轴 # 设置每一个年份的标题 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("1968-2019全球GDP前8.html")
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。