詳解用pyecharts Geo實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)熱力圖城市找不到問題解決
pyecharts 是一個(gè)用于生成 Echarts 圖表的類庫。 Echarts 是百度開源的一個(gè)數(shù)據(jù)可視化 JS 庫。主要用于數(shù)據(jù)可視化。
本文主要是用pycharts中的Geo繪制中國地圖,在圖中顯示出各個(gè)地區(qū)的人均銷售額
傳入的數(shù)據(jù)形如:[('上海',30), ('北京',50), ... ...]
li=[] for i,row in filtered.iterrows(): li.append((row['city'],int(row['per_capita']))) geo = Geo("sales per capita", "city", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') attr, value = geo.cast(li) geo.add("", attr, value, visual_range=[187, 820], visual_text_color="#fff", symbol_size=15, is_visualmap=True) geo.show_config() geo.render() geo = Geo("全國主要城市空氣質(zhì)量", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') attr, value = geo.cast(li) geo.add("", attr, value, type="heatmap", is_visualmap=True, visual_range=[200, 300], visual_text_color='#fff') geo.show_config() geo.render() geo = Geo("全國主要城市空氣質(zhì)量", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') attr, value = geo.cast(li) geo.add("", attr, value, type="effectScatter", is_random=True, effect_scale=5) geo.show_config() geo.render()
原來的包的問題是,經(jīng)緯度非常不全,一旦有找不到的,就畫不出來,方案一是把找不到的數(shù)據(jù)刪掉再畫
另一種辦法是到百度地圖api里把找不到的地方的經(jīng)緯度加進(jìn)原始的包里
搜索:百度地圖api-》地圖api示例-》地址解析
復(fù)制這些經(jīng)緯度;
打開pyecharts包里的base.py,找到記錄經(jīng)緯度信息的地方,把剛才的經(jīng)緯度信息補(bǔ)上去
如此便可以把所有數(shù)據(jù)都呈現(xiàn)在地圖上了
如果我想動(dòng)態(tài)選擇年份(2013-2017)以及選擇展現(xiàn)不同數(shù)據(jù)維度(人均消費(fèi)額,總消費(fèi)額)怎么辦?
這里要介紹一個(gè)python的模板引擎jinja2,該引擎仿照Django設(shè)計(jì)。模板是文本,用于分離文檔的形式和內(nèi)容,具體的介紹和用法可以看下面兩個(gè)鏈接
http://chabaoo.cn/article/163962.htm
http://docs.jinkan.org/docs/jinja2/templates.html
最基本的方法是通過Template創(chuàng)建模板并且渲染
from jinja2 import Template template = Template('Hello {{string}}!') template.render(string='world')
除了普通的字符串變量,jinja2還支持列表,字典和對(duì)象,
{{ mydict['key'] }} {{ mylist[3] }} {{ mylist[myintvar] }} {{ myobj.somemethod() }} {{myobj.someattribute}}
于是我們可以通過創(chuàng)建一個(gè)字典,將不同年份不同維度的數(shù)據(jù)都放入字典中,在展示數(shù)據(jù)時(shí),將指定數(shù)據(jù)傳入模板
options={}
for year in range(2013, 2018): options[year] = {} filtered = grouped[grouped['year'] == year] for dim in ('sales', 'per_capita'): li = [] for i, row in filtered.iterrows(): li.append((row['city'], int(row[dim]))) if dim == 'per_capita': geo = Geo(dim, "city (單位:元/人)", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') attr, value = geo.cast(li) geo.add("", attr, value, visual_range=[380, 450], visual_text_color="#fff", symbol_size=15, is_visualmap=True) else: geo = Geo(dim, "city (單位:百萬)", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59') attr, value = geo.cast(li) geo.add("", attr, value, visual_range=[10, 100], visual_text_color="#fff", symbol_size=15, is_visualmap=True) options[year][dim] = geo._option with open("template.html", encoding='utf-8') as f: template = jinja2.Template(f.read()) html = template.render(data=json.dumps(options)) with open("city_chart.html", "w") as f: f.write(html)
通過查看base.py里的render()可以看到傳入模板的是self._option
def render(self, path="render.html"): """ 渲染數(shù)據(jù)項(xiàng),生成 html 文件 :param path: 生成 html 文件保存路徑 """ from pyecharts import temple as Tp temple = Tp._temple series = self._option.get("series") for s in series: if s.get('type') == "wordCloud": temple = Tp._temple_wd break if s.get('type') == "liquidFill": temple = Tp._temple_lq break my_option = json.dumps(self._option, indent=4, ensure_ascii=False) __op = temple\ .replace("myOption", my_option)\ .replace("myWidth", str(self._width))\ .replace("myHeight", str(self._height)) try: # for Python3 with open(path, "w+", encoding="utf-8") as fout: fout.write(__op) except: # for Python2 with open(path, "w+") as fout: fout.write(__op)
template亦可仿照temple.py
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" rel="external nofollow" type="text/css"> <link rel="stylesheet" rel="external nofollow" type="text/css"> <link rel="external nofollow" rel="stylesheet"> <script src="https://cdn.bootcss.com/bootstrap-select/2.0.0-beta1/js/bootstrap-select.js"></script> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script> <script src="https://pingendo.com/assets/bootstrap/bootstrap-4.0.0-alpha.6.min.js"></script> <script src="http://oog4yfyu0.bkt.clouddn.com/echarts.min.js"></script> <script type="text/javascript " src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script> <script type="text/javascript " src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script> <script type="text/javascript " src="http://oog4yfyu0.bkt.clouddn.com/wordcloud.js"></script> ... ... </head> <body> <div class="py-5"> <div class="container"> <div class="row"> <div class="col-md-4"> <select class="selectpicker" id="year"> <option>2013</option> <option>2014</option> <option>2015</option> <option>2016</option> <option>2017</option> </select> </div> <div class="col-md-4"> <select class="selectpicker" id="dim"> <option value="sales">total_sales</option> <option value="per_capita">sales per capita</option> <option value="count">count</option> </select> </div> <div class="col-md-4"> <select class="selectpicker" id="customer_type"> <option value="new_customer">new_customer</option> <option value="old_customer">old_customer</option> <option value="members">members</option> </select> </div> <div class="col-md-4"> <a class="btn btn-default" href="#" rel="external nofollow" onclick="show()">refresh</a> </div> </div> <div class="row"> <div class='col-md-12'> <div id="main" style="width: 1200px;height:600px;"></div> </div> </div> </div> </div> <script type="text/javascript"> var data={{data}}; function show(){ var year=$("#year").val(); var dim=$("#dim").val(); var customer_type=$("#customer_type").val(); var myChart = echarts.init(document.getElementById('main')); var option=data[year][dim][customer_type]; option['tooltip']={'formatter':function(params){return params['name']+':'+params['value'][2]}}; myChart.setOption(option); } $(show);//加載完文檔之后運(yùn)行這個(gè)函數(shù) </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用python進(jìn)行nc轉(zhuǎn)tif的3種情況解決
在進(jìn)行氣候分析時(shí),很多人都會(huì)用到ERA5數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于如何使用python進(jìn)行nc轉(zhuǎn)tif的3種情況的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03淺談python中的__init__、__new__和__call__方法
這篇文章主要給大家介紹了關(guān)于python中__init__、__new__和__call__方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考學(xué)習(xí),下面來跟著小編一起看看吧。2017-07-07Python實(shí)現(xiàn)圖數(shù)據(jù)處理的完整指南
圖是一種非常重要的數(shù)據(jù)結(jié)構(gòu),在Python中,我們可以使用鄰接矩陣來表示圖,這篇文章主要為大家介紹了Python實(shí)現(xiàn)圖數(shù)據(jù)處理的相關(guān)知識(shí),需要的可以參考下2024-04-04對(duì)Python 文件夾遍歷和文件查找的實(shí)例講解
下面小編就為大家分享一篇對(duì)Python 文件夾遍歷和文件查找的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python使用pymupdf實(shí)現(xiàn)PDF加密
這篇文章主要介紹了如何使用 Python 和 wxPython 庫創(chuàng)建一個(gè)簡單的圖形用戶界面(GUI)應(yīng)用程序,用于對(duì) PDF 文件進(jìn)行加密,感興趣的小伙伴可以了解下2023-08-08在Windows中設(shè)置Python環(huán)境變量的實(shí)例講解
下面小編就為大家分享一篇在Windows中設(shè)置Python環(huán)境變量的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python中tkinter+MySQL實(shí)現(xiàn)增刪改查
這篇文章主要介紹了Python中tkinter+MySQL實(shí)現(xiàn)增刪改查,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04