# # 導入數據處理的包
# # from pyecharts.charts import Line
# # from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts, LabelOpts
# # import json
# #
# # # 打開文件
# # file_us = open("美國.txt", "r", encoding="UTF-8")
# # file_jp = open("日本.txt", "r", encoding="UTF-8")
# # file_in = open("印度.txt", "r", encoding="UTF-8")
# #
# # # 讀文件
# # us_data = file_us.read()
# # jp_data = file_jp.read()
# # in_data = file_in.read()
# #
# # # 規整文件內容
# # us_data = us_data.replace("jsonp_1629344292311_69436(", "")
# # jp_data = jp_data.replace("jsonp_1629350871167_29498(", "")
# # in_data = in_data.replace("jsonp_1629350745930_63180(", "")
# #
# # us_data = us_data[:-2:]
# # jp_data = jp_data[:-2:]
# # in_data = in_data[:-2:]
# #
# # # json 轉化為字典
# # us_data = json.loads(us_data)
# # jp_data = json.loads(jp_data)
# # in_data = json.loads(in_data)
# #
# # # 得到trend數據
# # us_trend_data = us_data["data"][0]["trend"]
# # jp_trend_data = jp_data["data"][0]["trend"]
# # in_trend_data = in_data["data"][0]["trend"]
# #
# # # 得到x軸的數據(x軸是2020全年)
# # us_x_data = us_trend_data["updateDate"][:314]
# # jp_x_data = jp_trend_data["updateDate"][:314]
# # in_x_data = in_trend_data["updateDate"][:314]
# #
# # # 得到y軸的數據(y軸數據是三個國家的數據)
# # us_y_data = us_trend_data["list"][0]["data"][:314]
# # jp_y_data = jp_trend_data["list"][0]["data"][:314]
# # in_y_data = in_trend_data["list"][0]["data"][:314]
# #
# # # 開始繪圖
# # # 創建一個圖
# # line = Line()
# #
# # # 創建圖的x軸
# # line.add_xaxis(us_x_data)
# #
# # # 創建圖的y軸
# # line.add_yaxis("美國確診人數是", us_y_data, label_opts=LabelOpts(is_show=False))
# # line.add_yaxis("日本的確診人數是", jp_y_data, label_opts=LabelOpts(is_show=False))
# # line.add_yaxis("印度的確診人數是", in_y_data, label_opts=LabelOpts(is_show=False))
# #
# # # 配置圖的全局設置
# # line.set_global_opts(
# # title_opts=TitleOpts(title="2020年美國、日本、印度新冠日感染人數", pos_left="center", pos_bottom="1%"),
# # legend_opts=LegendOpts(is_show=True),
# # toolbox_opts=ToolboxOpts(is_show=True),
# # visualmap_opts=VisualMapOpts(is_show=True)
# # )
# #
# # # 生成圖
# # line.render()
# #
# # # 關閉文件
# # file_us.close()
# # file_jp.close()
# # file_in.close()
#
#
# # 導入要用到的包
# import json
# from pyecharts.charts import Line
# from pyecharts.options import LabelOpts, TitleOpts, ToolboxOpts, VisualMapOpts, LegendOpts
#
# # 讀文件
# file_us = open("美國.txt", "r", encoding="UTF-8")
# file_jp = open("日本.txt", "r", encoding="UTF-8")
# file_in = open("印度.txt", "r", encoding="UTF-8")
# us_data = file_us.read()
# jp_data = file_jp.read()
# in_data = file_in.read()
#
# # 讀取完畢,關閉文件
# file_us.close()
# file_jp.close()
# file_in.close()
#
# # 規整文件內容
# us_data = us_data.replace("jsonp_1629344292311_69436(", "")
# jp_data = jp_data.replace("jsonp_1629350871167_29498(", "")
# in_data = in_data.replace("jsonp_1629350745930_63180(", "")
# us_data = us_data[:-2:]
# jp_data = jp_data[:-2:]
# in_data = in_data[:-2:]
#
# # json轉化為字典
# us_data = json.loads(us_data)
# jp_data = json.loads(jp_data)
# in_data = json.loads(in_data)
#
# # 得到每個國家的trend
# us_trend = us_data["data"][0]["trend"]
# jp_trend = jp_data["data"][0]["trend"]
# in_trend = in_data["data"][0]["trend"]
#
# # 得到x軸內容
# us_x = us_trend["updateDate"][:314:]
# jp_x = jp_trend["updateDate"][:314:]
# in_x = in_trend["updateDate"][:314:]
#
# # 得到y軸內容
# us_y = us_trend["list"][0]["data"][:314:]
# jp_y = jp_trend["list"][0]["data"][:314:]
# in_y = in_trend["list"][0]["data"][:314:]
#
# #開始畫線
# line = Line()
#
# # 創建x軸
# line.add_xaxis(us_x)
#
# # 創建y軸
# line.add_yaxis("美國確診新冠的人數", us_y, label_opts=LabelOpts(is_show=False))
# line.add_yaxis("日本確診新冠的人數", jp_y, label_opts=LabelOpts(is_show=False))
# line.add_yaxis("印度確診新冠的人數", in_y, label_opts=LabelOpts(is_show=False))
#
# # 調整全局設置
# line.set_global_opts(
# title_opts=TitleOpts(title="2020年美國、日本、印度每日確診新冠人數", pos_left="center", pos_bottom="1%"),
# legend_opts=LegendOpts(is_show=True),
# toolbox_opts=ToolboxOpts(is_show=True),
# visualmap_opts=VisualMapOpts(is_show=True)
# )
#
# # 生成圖
# line.render()# # 導入地圖構建使用的包
# from pyecharts.charts import Map
# from pyecharts.options import VisualMapOpts
#
# # 創建一個地圖
# China_map = Map()
#
# # 給地圖準備數據
# data = [
# ("北京市", 100),
# ("上海市", 95),
# ("深圳市", 90),
# ("重慶市", 80),
# ("四川省", 15)
# ]
#
# # 給地圖添加數據
# China_map.add("中國城市發展地圖", data, "china")
#
# # 設置全局變量
# China_map.set_global_opts(
# visualmap_opts=VisualMapOpts(
# is_show=True,
# is_piecewise=True,
# pieces=[
# {"min": 1, "max": 19, "label": "1-19", "color": "#CCFFFF"},
# {"min": 20, "max": 39, "label": "20-39", "color": "#FFFF99"},
# {"min": 40, "max": 59, "label": "40-59", "color": "#FF9966"},
# {"min": 60, "max": 79, "label": "60-79", "color": "#FF6666"},
# {"min": 80, "max": 100, "label": "80-99", "color": "#CC3333"}
# ]
# )
# )
#
# # 生成地圖
# China_map.render()# # 導入疫情地圖需要的包
# import json
# from pyecharts.charts import Map
# from pyecharts.options import VisualMapOpts
#
# # 打開需要的數據文件
# China_map = open("疫情.txt", "r", encoding="UTF-8")
# Ch_map = China_map.read()
#
# # 關閉文件
# China_map.close()
#
# # 得到所有地區數據
# area_map = json.loads(Ch_map)
# area_map = area_map["areaTree"][0]["children"]
#
# # 將數據成對封裝成元組,然后再將元組封裝至列表中,然后用列表給地圖提供數據
# China_list = []
# for province in area_map:
# province_name = province["name"] + "省"
# province_confirm = province["total"]["confirm"]
#
# China_list.append((province_name, province_confirm))
#
# # 創建一個疫情地圖
# China_Map = Map()
#
# # 為疫情地圖加入數據
# China_Map.add("中國全國新冠確診人數圖", China_list, "china")
#
# # 為疫情地圖調整全局設置,使得更加美觀
# 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": 1999, "label": "100-1999人確診", "color": "#FFFF99"},
# {"min": 2000, "max": 9999, "label": "2000-9999人確診", "color": "#FF9966"},
# {"min": 10000, "max": 39999, "label": "10000-39999人確診", "color": "#FF6666"},
# {"min": 40000, "label": "40000人以上確診", "color": "#CC3333"}
# ]
# )
# )
#
# # 生成疫情地圖
# China_Map.render()# 導入生成重慶疫情地圖的包
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts# 讀數據文件
cq_file = open("疫情.txt", "r", encoding="UTF-8")
cq_data = cq_file.read()# 關閉文件
cq_file.close()# 將數據定位到重慶
cq_data = json.loads(cq_data)
cq_data = cq_data["areaTree"][0]["children"][18]["children"]# 將區縣名字取出,和每個區縣的確診人數構成元組存入列表
cq_list = []
for area in cq_data:area_name = area["name"]area_confirm = area["total"]["confirm"]cq_list.append((area_name, area_confirm))# 創造一個地圖
cq_map = Map()# 向重慶疫情地圖中加入值
cq_map.add("重慶新冠確診人數圖", cq_list, "重慶")# 調整重慶疫情地圖的全局設置(主要是改變顏色)
cq_map.set_global_opts(visualmap_opts=VisualMapOpts(is_show=True,is_piecewise=True,pieces=[{"min": 1, "max": 9, "label": "1-9人確診", "color": "#FFFF99"},{"min": 10, "max": 19, "label": "10-19人確診", "color": "#FF9966"},{"min": 20, "max": 29, "label": "20-29人確診", "color": "#FF6666"},{"min": 30, "label": "30人以上確診", "color": "#CC3333"},])
)# 生成重慶的疫情地圖
cq_map.render()#
# # 導入柱狀圖的包
# from pyecharts.charts import Bar
#
# # 創建一個柱狀圖
# bar1 = Bar()
# bar1.add_xaxis(["中國", "美國", "英國"])
# bar1.add_yaxis("1900年GDP總量", [10, 20, 30])
#
# # 生成圖
# bar1.render()
#
# # 導入時間圖的包
# from pyecharts.charts import Bar
# from pyecharts.options import VisualMapOpts
# from pyecharts.charts import Timeline
# from pyecharts.globals import ThemeType
#
# # 根據時間點創建多個坐標圖
# bar1 = Bar()
# bar1.add_xaxis(["中國", "美國", "英國"])
# bar1.add_yaxis("GDP", [10, 20, 30])
#
# bar2 = Bar()
# bar2.add_xaxis(["中國", "美國", "英國"])
# bar2.add_yaxis("GDP", [40, 120, 85])
#
# bar3 = Bar()
# bar3.add_xaxis(["中國", "美國", "英國"])
# bar3.add_yaxis("GDP", [500, 600, 430])
#
# # 創建時間軸
# timeline = Timeline({"theme": ThemeType.LIGHT})
# timeline.add(bar1, "1900年GDP總量")
# timeline.add(bar2, "1949年GDP總量")
# timeline.add(bar3, "2024年GDP總量")
#
# # 調整時間軸播放
# timeline.add_schema(
# play_interval=3000,
# is_timeline_show=True,
# is_auto_play=True,
# is_loop_play=True
# )
#
# # 生成時間軸圖
# timeline.render("從清朝到現在的三國GDP變化.html")# 最后一舞(1960-2019年全球GDP top8變化圖)
# 導包
from pyecharts.charts import Bar, Timeline
from pyecharts.options import LabelOpts, TitleOpts
from pyecharts.globals import ThemeType# 讀取數據文件信息
GDP_f = open("1960-2019全球GDP數據.csv", "r", encoding="GB2312")
GDP_data = GDP_f.readlines() # GDP_data 是列表# 關閉文件
GDP_f.close()# 規整數據文件信息
GDP_data.pop(0)# 定義一個字典來存儲數據
GDP_data_dict = {}
for line in GDP_data:year = int(line.split(",")[0]) # 得到年份country = line.split(",")[1] # 得到國家GDP = float(line.split(",")[2]) # 得到GDP(因為有些數字是科學計數法,所以說轉化為float來用)try:GDP_data_dict[year].append([country, GDP])except KeyError:GDP_data_dict[year] = []GDP_data_dict[year].append([country, GDP])# 構建timeline對象
timeline = Timeline({"theme": ThemeType.LIGHT})# 排序數據對象
sort_year = sorted(GDP_data_dict.keys())for years in sort_year:GDP_data_dict[years].sort(key=lambda element: element[1], reverse=True)year_data = GDP_data_dict[years][0:8] # 只取得某一年中GDP前八的國家x_data = []y_data = []for country_gdp in year_data: # 從GDP前8的國家里面再取,作為柱狀圖的x-y軸x_data.append(country_gdp[0]) # 國家名字y_data.append(country_gdp[1] / 100000000) # 國家當年的GDP# 建立柱狀圖bar = Bar()x_data.reverse()y_data.reverse()bar.add_xaxis(x_data)bar.add_yaxis("GDP(億)", y_data, label_opts=LabelOpts(position="right"))# 反轉x-y軸bar.reversal_axis()# 設置每一年的標題bar.set_global_opts(title_opts=TitleOpts(title=f"{years}年全球前八國家的GDP"))# 創建時間線timeline.add(bar, str(years)) # 添加一個時間線# 調整時間軸播放
timeline.add_schema(play_interval=3000, # 時間移動的時間is_timeline_show=True, # 展示時間線is_auto_play=True, # 自動播放is_loop_play=True # 循環播放
)# 生成柱狀圖
timeline.render("1960-2019年全球GDP top8變化圖.html")