本文主要参考:http://www.jianshu.com/p/773ff5f08a2c
年初,国家统计局公布了70个大中城市住宅销售价格变动情况
其中有一项为价格指数表:
为了直观显示热度分布,这里直接用了百度官网的API,恰好里面有heatmap.js。
网址为:
http://developer.baidu.com/map/jsdemo.htm#c1_15
从中可以看出,数组points的规范为:
var points =[
{"lng":116.418261,"lat":39.921984,"count":50},
{"lng":116.423332,"lat":39.916532,"count":51},
......
];
因此,我们最终python程序要将数据规整为上面points的表现形式。
这里我们采用同比数据,如下csv文件所示:
北京,127.0
天津,124.4
石家庄,118.9
太原,103.0
呼和浩特,101.0
沈阳,103.2
大连,102.5
长春,104.4
哈尔滨,102.1
上海,128.3
南京,137.3
杭州,127.6
宁波,111.2
合肥,144.2
福州,125.7
厦门,138.8
南昌,114.5
济南,119.0
青岛,113.2
郑州,127.7
武汉,124.2
长沙,118.4
广州,124.2
深圳,118.4
南宁,111.2
海口,106.5
重庆,107.7
成都,105.3
贵阳,105.3
昆明,104.2
西安,107.6
兰州,103.2
西宁,102.3
银川,102.4
乌鲁木齐,99.1
唐山,103.2
秦皇岛,107.0
包头,100.2
丹东,99.8
锦州,97.7
吉林,102.6
牡丹江,99.3
无锡,134.4
扬州,110.3
徐州,109.6
温州,104.4
金华,106.8
蚌埠,109.8
安庆,107.7
泉州,109.8
九江,112.2
赣州,114.0
烟台,105.5
济宁,102.0
洛阳,105.0
平顶山,103.5
宜昌,105.9
襄阳,103.2
岳阳,105.8
常德,103.3
惠州,124.7
湛江,109.3
韶关,108.7
桂林,103.8
北海,104.5
三亚,106.3
泸州,103.0
南充,102.0
遵义,102.0
大理,102.6
接下来我们便希望得到上述城市在地图中对应的经纬度,也就是points变量里面的lng、lat以及count信息。
在撰写python程序之前需要去官网获取ak码,登陆http://lbsyun.baidu.com/,在该网页最下方即可申请密匙。(ip可以写成0.0.0.0/0)
对应的python程序如下:
# -*- coding: UTF-8 -*-
#import requests
#from lxml import etree
#import pandas as pd
import csv
import json
import urllib
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def getlonlat(address):
ak = '你的百度api 的 key'
ori_url ='http://api.map.baidu.com/geocoder/v2/'
url = ori_url +'?address='+address+'&output=json'+'&ak='+ak
req = urllib.urlopen(url)
#urlopen返回 一个类文件对象(基础网页爬虫知识啦)
res = req.read()
temp = json.loads(res)
# print req
# print res
# print temp
return temp
# add ='山西'
# getlonlat(add)
file=open('longlat.json','w')
#在本地生成爬取的经纬度信息,以json格式保存
with open('price.csv','r') as csvfile:
#打开本地的price.csv文件
reader = csv.reader(csvfile)
for line in reader:
city = line[0].strip()
pricenum = line[1].strip()
# print city,pricenum
lon = getlonlat(city)['result']['location']['lng']
lat = getlonlat(city)['result']['location']['lat']
str_temp = '{"lat":' + str(lat) + ',"lng":' + str(lon) + ',"count":' + str(pricenum) +'},'
print str_temp
#规整后的数据
# print '"city":'+str(city)+',"lat":' + str(lat) + ',"lng":' + str(lon) + ',"count":' + str(pricenum) +''
file.write(str_temp+'\n')
file.close()
最终得到数据如下:
{"lat":39.9299857781,"lng":116.395645038,"count":127.0},
{"lat":39.1439299033,"lng":117.210813092,"count":124.4},
{"lat":38.0489583146,"lng":114.522081844,"count":118.9},
{"lat":37.890277054,"lng":112.550863589,"count":103.0},
{"lat":40.8283188731,"lng":111.66035052,"count":101.0},
{"lat":41.8086447835,"lng":123.432790922,"count":103.2},
{"lat":38.9487099383,"lng":121.593477781,"count":102.5},
{"lat":43.8983376071,"lng":125.313642427,"count":104.4},
{"lat":45.7732246332,"lng":126.657716855,"count":102.1},
{"lat":31.24916171,"lng":121.487899486,"count":128.3},
{"lat":32.0572355018,"lng":118.778074408,"count":137.3},
{"lat":30.2592444615,"lng":120.219375416,"count":127.6},
{"lat":29.8852589659,"lng":121.579005973,"count":111.2},
{"lat":31.8669422607,"lng":117.282699092,"count":144.2},
{"lat":26.0471254966,"lng":119.330221107,"count":125.7},
{"lat":24.4892306125,"lng":118.103886046,"count":138.8},
{"lat":28.6895780001,"lng":115.893527546,"count":114.5},
{"lat":36.6827847272,"lng":117.024967066,"count":119.0},
{"lat":36.1052149013,"lng":120.384428184,"count":113.2},
{"lat":34.7566100641,"lng":113.64964385,"count":127.7},
{"lat":30.5810841269,"lng":114.316200103,"count":124.2},
{"lat":28.2134782309,"lng":112.979352788,"count":118.4},
{"lat":23.1200491021,"lng":113.307649675,"count":124.2},
{"lat":22.5460535462,"lng":114.025973657,"count":118.4},
{"lat":22.8064929356,"lng":108.297233556,"count":111.2},
{"lat":20.022071277,"lng":110.330801848,"count":106.5},
{"lat":29.5446061089,"lng":106.530635013,"count":107.7},
{"lat":30.6799428454,"lng":104.067923463,"count":105.3},
{"lat":26.6299067414,"lng":106.709177096,"count":105.3},
{"lat":25.0491531005,"lng":102.714601139,"count":104.2},
{"lat":42.9863649464,"lng":125.150148579,"count":107.6},
{"lat":36.064225525,"lng":103.823305441,"count":103.2},
{"lat":36.640738612,"lng":101.76792099,"count":102.3},
{"lat":38.5026210119,"lng":106.206478608,"count":102.4},
{"lat":43.8403803472,"lng":87.5649877411,"count":99.1},
{"lat":39.6505309225,"lng":118.183450598,"count":103.2},
{"lat":39.9454615659,"lng":119.604367616,"count":107.0},
{"lat":40.6471194257,"lng":109.846238532,"count":100.2},
{"lat":40.1290228266,"lng":124.338543115,"count":99.8},
{"lat":41.1308788759,"lng":121.147748738,"count":97.7},
{"lat":43.8719883344,"lng":126.564543989,"count":102.6},
{"lat":44.5885211528,"lng":129.608035396,"count":99.3},
{"lat":31.5700374519,"lng":120.305455901,"count":134.4},
{"lat":32.4085052546,"lng":119.427777551,"count":110.3},
{"lat":34.2715534311,"lng":117.188106623,"count":109.6},
{"lat":28.002837594,"lng":120.690634734,"count":104.4},
{"lat":29.1028991054,"lng":119.652575704,"count":106.8},
{"lat":32.9294989067,"lng":117.357079866,"count":109.8},
{"lat":30.5378978174,"lng":117.058738772,"count":107.7},
{"lat":24.901652384,"lng":118.600362343,"count":109.8},
{"lat":29.7196395261,"lng":115.999848022,"count":112.2},
{"lat":25.8452955363,"lng":114.935909079,"count":114.0},
{"lat":37.5365615629,"lng":121.30955503,"count":105.5},
{"lat":35.4021216643,"lng":116.600797625,"count":102.0},
{"lat":34.6573678177,"lng":112.447524769,"count":105.0},
{"lat":33.7453014565,"lng":113.300848978,"count":103.5},
{"lat":30.732757818,"lng":111.310981092,"count":105.9},
{"lat":32.2291685915,"lng":112.250092848,"count":103.2},
{"lat":29.3780070755,"lng":113.146195519,"count":105.8},
{"lat":29.0121488552,"lng":111.653718137,"count":103.3},
{"lat":23.1135398524,"lng":114.41065808,"count":124.7},
{"lat":21.2574631038,"lng":110.365067263,"count":109.3},
{"lat":24.8029603119,"lng":113.594461107,"count":108.7},
{"lat":25.262901246,"lng":110.260920147,"count":103.8},
{"lat":21.472718235,"lng":109.122627919,"count":104.5},
{"lat":18.2577759149,"lng":109.522771281,"count":106.3},
{"lat":28.8959298039,"lng":105.443970289,"count":103.0},
{"lat":30.8009651682,"lng":106.105553984,"count":102.0},
{"lat":27.6999613771,"lng":106.931260316,"count":102.0},
{"lat":25.6939666225,"lng":100.219208954,"count":102.6},
将以上数据填充在百度热力图编辑器里面的points变量里即可得到最终的房价涨幅热力图数据:
关于heatmap.js
其中的参数可以自行调整。
backgroundColor 画板的背景颜色设置,支持rgb(a),颜色名称,但必须要用引号
gradient 设置热点图的光圈颜色,数值为[0,1],数值大的在光圈内侧,数值相等则靠下的生效,数值设置不分大小顺序,并可以同时设置很多颜色
radius 设置光圈的半径大小,值>=0,=0取得是默认值
opacity 光圈透明度设置[0,1],如果值设置了,会重写maxOpacity和minOpacity的值
很惭愧就做了一点微小的整理!