詳解用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://www.dhdzp.com/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)文章
幫你快速上手Jenkins并實(shí)現(xiàn)自動(dòng)化部署
在未學(xué)習(xí)Jenkins之前,只是對(duì)Jenkins有一個(gè)比較模糊的理解,即Jenkins是一個(gè)自動(dòng)化構(gòu)建項(xiàng)目發(fā)布的工具,可以實(shí)現(xiàn)代碼->github或者gitlab庫->jenkins自動(dòng)部署->訪問的整體的過程,而無需人為重新打包,今天就帶大家詳細(xì)了解一下,幫你快速上手Jenkins,需要的朋友可以參考下2021-06-06
Python新手們?nèi)菀追傅膸讉€(gè)錯(cuò)誤總結(jié)
python語言里面有一些小的坑,特別容易弄混弄錯(cuò),初學(xué)者若不注意的話,很容易坑進(jìn)去,下面我給大家深入解析一些這幾個(gè)坑,希望對(duì)初學(xué)者有所幫助,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04
Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換詳解
這篇文章主要介紹了Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換詳解,Base64是一種二進(jìn)制到文本的編碼方式,如果要更具體一點(diǎn)的話,可以認(rèn)為它是一種將 byte數(shù)組編碼為字符串的方法,而且編碼出的字符串只包含ASCII基礎(chǔ)字符,需要的朋友可以參考下2024-01-01
可視化pytorch 模型中不同BN層的running mean曲線實(shí)例
這篇文章主要介紹了可視化pytorch 模型中不同BN層的running mean曲線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
使用PyOpenGL繪制三維坐標(biāo)系實(shí)例
今天小編就為大家分享一篇使用PyOpenGL繪制三維坐標(biāo)系實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
對(duì)python中dict和json的區(qū)別詳解
今天小編就為大家分享一篇對(duì)python中dict和json的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python flask解析json數(shù)據(jù)不完整的解決方法
這篇文章主要介紹了python flask解析json數(shù)據(jù)不完整的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
python的描述符(descriptor)、裝飾器(property)造成的一個(gè)無限遞歸問題分享
這篇文章主要介紹了python的描述符(descriptor)、裝飾器(property)造成的一個(gè)無限遞歸問題分享,一個(gè)不太會(huì)遇到的問題,需要的朋友可以參考下2014-07-07
Python中for循環(huán)語句實(shí)戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Python中for循環(huán)語句的相關(guān)資料,python中for循環(huán)一般用來迭代字符串,列表,元組等,當(dāng)for循環(huán)用于迭代時(shí)不需要考慮循環(huán)次數(shù),循環(huán)次數(shù)由后面的對(duì)象長度來決定,需要的朋友可以參考下2023-09-09

