黑馬程序員python教程,8天python從入門到精通,學python看這套就


第105節(jié),你們的地圖也是這樣子的嗎?
from pyecharts.charts import Map from pyecharts.options import VisualMapOpts # 數(shù)據(jù)對象創(chuàng)建 map = Map() # 數(shù)據(jù)準備 data =[ ("北京市",99), ("上海市",199), ("廣動省",399), ("江蘇省",699), ("山東省",999) ] # 數(shù)據(jù)添加 map.add("地圖",data,"china",) # 設置全局的數(shù)據(jù) map.set_global_opts( visualmap_opts=VisualMapOpts( is_show=True, is_piecewise=True, # 數(shù)據(jù)范圍校準 pieces=[ {"min":1 ,"max": 9,"label":"1-9人", "color":"#CCFFFF"}, {"min":10 ,"max": 99,"label":"10-99人", "color":"#FFFF99"}, {"min":100 ,"max":499 ,"label":"100-499人", "color":"#FF9966"}, {"min":500 ,"max": 999,"label":"500-999人", "color":"#FF6666"}, {"min":1000 ,"max": 9999,"label":"1000-9999人", "color":"#CC3333"}, {"min":10000,"label":"10000以上", "color":"#990033"} ]) ) # 數(shù)據(jù)生成 map.render() #
P106 數(shù)據(jù)補充,最笨的方法
import json from pyecharts.charts import Map from pyecharts.options import LabelOpts from pyecharts.options import * from pyecharts import options as opts fc = open("D:/疫情.txt",'r',encoding='UTF-8') fc_data = fc.read() fc_json = json.loads(fc_data) fc_province_list = fc_json["areaTree"][0]["children"] # 接收需要的數(shù)據(jù) data_list = [] # 循環(huán)獲取需要的數(shù)據(jù) for province_list in fc_province_list : province_name = province_list["name"] if province_name == "新疆": province_name = province_name+"維吾爾自治區(qū)" elif province_name == "西藏": province_name = province_name + "自治區(qū)" elif province_name == "廣西": province_name = province_name + "壯族自治區(qū)" elif province_name == "重慶" or province_name == "北京" or province_name == "天津": province_name = province_name + "市" elif province_name == "內(nèi)蒙古" : province_name = province_name + "自治區(qū)" elif province_name == "寧夏": province_name = province_name + "回族自治區(qū)" elif province_name == "香港" or province_name == "澳門": province_name = province_name + "特別行政區(qū)" else: province_name = province_name + "省" province_confirm = province_list["total"]["confirm"] data_list.append((province_name,province_confirm)) map = Map() map.add("個省份確診人數(shù)",data_list,"china") #全局選項 map.set_global_opts( title_opts=TitleOpts("全國疫情地圖"), visualmap_opts=VisualMapOpts( is_show=True, is_piecewise=True, pieces=[ {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"}, {"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"}, {"min": 100, "max": 499, "label": "100-499人", "color": "#FF9966"}, {"min": 500, "max": 999, "label": "500-999人", "color": "#FF6666"}, {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"}, {"min": 10000, "label": "10000以上", "color": "#990033"} ] ) ) map.render() fc.close() from pyecharts.charts import Bar from pyecharts.options import * from pyecharts.charts import Timeline from pyecharts.globals import ThemeType # 文件讀取 f = open("D:/1960-2019全球GDP數(shù)據(jù).csv",'r',encoding="GB2312") # 讀取所有行,并接收 data_lines = f.readlines() # 關閉文件 f.close() # 刪除第一條數(shù)據(jù) data_lines.pop(0) # 數(shù)據(jù)獲取 data_dict = {} # 獲取每行的數(shù)據(jù) for lines in data_lines: year = int(lines.split(",")[0]) country = lines.split(",")[1] gdp = float(lines.split(",")[2]) try: data_dict[year].append([country,gdp]) except KeyError: data_dict[year] = [] data_dict[year].append([country,gdp]) # print(data_dict) # 排序年份 sort_list_year = sorted(data_dict.keys()) # 創(chuàng)建時間線對象 timeLine = Timeline({"theme":ThemeType.LIGHT}) # 循環(huán)獲取國家數(shù)據(jù) for year in sort_list_year: # 取出前8的國家 data_dict[year].sort(key=lambda element:element[1],reverse=True) year_date = data_dict[year][0:8] # 構建柱狀圖 x_data =[] y_data =[] for country_gdp in year_date: x_data.append(country_gdp[0]) y_data.append(country_gdp[1]/100000000) # 構建柱狀圖 bar = Bar() # 數(shù)據(jù)反轉 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"{year}年全球前8GDP數(shù)據(jù)") ) timeLine.add(bar,str(year)) timeLine.add_schema( play_interval=500, is_timeline_show=True, is_auto_play=True, is_loop_play=False ) timeLine.render("1960-2014年全球GDP數(shù)據(jù).html")
標簽: