自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

10行Python代碼創(chuàng)建可視化地圖

開(kāi)發(fā) 后端
當(dāng)我開(kāi)始建造Vincent時(shí), 我的一個(gè)目的就是使得地圖的建造盡可能合理化. 有一些很棒的python地圖庫(kù)-參見(jiàn)Basemap 和 Kartograph能讓地圖更有意思。我想有更簡(jiǎn)單一些的工具,能依靠Vega的力量并且允許簡(jiǎn)單的語(yǔ)法點(diǎn)到geoJSON文件,詳細(xì)描述一個(gè)投影和大小/比列,最后輸出地圖。

?[[202326]]

import vincent

world_countries = r'world-countries.json'

world = vincent.Map(width=1200, height=1000)

world.geo_data(projection='winkel3', scale=200, world=world_countries)

world.to_json(path)

 

當(dāng)我開(kāi)始建造Vincent時(shí), 我的一個(gè)目的就是使得地圖的建造盡可能合理化. 有一些很棒的python地圖庫(kù)-參見(jiàn)Basemap 和 Kartograph能讓地圖更有意思. 我強(qiáng)烈推薦這兩個(gè)工具, 因?yàn)樗麄兌己芎糜枚液軓?qiáng)大. 我想有更簡(jiǎn)單一些的工具,能依靠Vega的力量并且允許簡(jiǎn)單的語(yǔ)法點(diǎn)到geoJSON文件,詳細(xì)描述一個(gè)投影和大小/比列,***輸出地圖。

例如, 將地圖數(shù)據(jù)分層來(lái)建立更復(fù)雜的地圖:

vis = vincent.Map(width=1000, height=800)

#Add the US county data and a new line color

vis.geo_data(projection='albersUsa', scale=1000, counties=county_geo)

vis + ('2B4ECF', 'marks', 0, 'properties', 'enter', 'stroke', 'value')

#Add the state data, remove the fill, write Vega spec output to JSON

vis.geo_data(states=state_geo)

vis - ('fill', 'marks', 1, 'properties', 'enter')

vis.to_json(path)

?

加之,等值線地圖需綁定Pandas數(shù)據(jù),需要數(shù)據(jù)列直接映射到地圖要素.假設(shè)有一個(gè)從geoJSON到列數(shù)據(jù)的1:1映射,它的語(yǔ)法是非常簡(jiǎn)單的:

#'merged' is the Pandas DataFrame

vis = vincent.Map(width=1000, height=800)

vis.tabular_data(merged, columns=['FIPS_Code', 'Unemployment_rate_2011'])

vis.geo_data(projection='albersUsa', scale=1000, bind_data='data.id', counties=county_geo)

vis + (["#f5f5f5","#000045"], 'scales', 0, 'range')

vis.to_json(path)

?

我們的數(shù)據(jù)并非沒(méi)有爭(zhēng)議無(wú)需改造——用戶(hù)需要確保 geoJSON 鍵與熊貓數(shù)據(jù)框架之間具有1:1的映射。下面就是之前實(shí)例所需的簡(jiǎn)明的數(shù)據(jù)框架映射:我們的國(guó)家信息是一個(gè)列有FIPS 碼、國(guó)家名稱(chēng)、以及經(jīng)濟(jì)信息(列名省略)的 CSV 文件:

00000,US,United States,154505871,140674478,13831393,9,50502,100

01000,AL,Alabama,2190519,1993977,196542,9,41427,100

01001,AL,Autauga County,25930,23854,2076,8,48863,117.9

01003,AL,Baldwin County,85407,78491,6916,8.1,50144,121

01005,AL,Barbour County,9761,8651,1110,11.4,30117,72.7

在 geoJSON 中,我們的國(guó)家形狀是以 FIPS 碼為id 的(感謝 fork 自 Trifacta 的相關(guān)信息)。為了簡(jiǎn)便,實(shí)際形狀已經(jīng)做了簡(jiǎn)略,在示例數(shù)據(jù)可以找到完整的數(shù)據(jù)集:

{"type":"FeatureCollection","features":[

{"type":"Feature","id":"1001","properties":{"name":"Autauga"}

{"type":"Feature","id":"1003","properties":{"name":"Baldwin"}

{"type":"Feature","id":"1005","properties":{"name":"Barbour"}

{"type":"Feature","id":"1007","properties":{"name":"Bibb"}

{"type":"Feature","id":"1009","properties":{"name":"Blount"}

{"type":"Feature","id":"1011","properties":{"name":"Bullock"}

{"type":"Feature","id":"1013","properties":{"name":"Butler"}

{"type":"Feature","id":"1015","properties":{"name":"Calhoun"}

{"type":"Feature","id":"1017","properties":{"name":"Chambers"}

{"type":"Feature","id":"1019","properties":{"name":"Cherokee"}

我們需要匹配 FIPS 碼,確保匹配正確,否則 Vega 無(wú)法正確的壓縮數(shù)據(jù):

import json
import pandas as pd
#Map the county codes we have in our geometry to those in the
#county_data file, which contains additional rows we don't need
with open(county_geo, 'r') as f:
get_id = json.load(f)

#Grab the FIPS codes and load them into a dataframe
county_codes = [x['id'] for x in get_id['features']]
county_df = pd.DataFrame({'FIPS_Code': county_codes}, dtype=str)

#Read into Dataframe, cast to string for consistency
df = pd.read_csv(county_data, na_values=[' '])
df['FIPS_Code'] = df['FIPS_Code'].astype(str)

#Perform an inner join, pad NA's with data from nearest county
merged = pd.merge(df, county_df, on='FIPS_Code', how='inner')
merged = merged.fillna(method='pad')

>>>merged.head()
FIPS_Code State Area_name Civilian_labor_force_2011 Employed_2011 \
0 1001 AL Autauga County 25930 23854
1 1003 AL Baldwin County 85407 78491
2 1005 AL Barbour County 9761 8651
3 1007 AL Bibb County 9216 8303
4 1009 AL Blount County 26347 24156

Unemployed_2011 Unemployment_rate_2011 Median_Household_Income_2011 \
0 2076 8.0 48863
1 6916 8.1 50144
2 1110 11.4 30117
3 913 9.9 37347
4 2191 8.3 41940

Med_HH_Income_Percent_of_StateTotal_2011
0 117.9
1 121.0
2 72.7
3 90.2
4 101.2

現(xiàn)在,我們可以快速生成不同的等值線:

vis.tabular_data(merged, columns=['FIPS_Code', 'Civilian_labor_force_2011'])

vis.to_json(path)

 

?

這只能告訴我們 LA 和 King 面積非常大,人口非常稠密。讓我們?cè)倏纯粗械燃彝ナ杖耄?/p>

vis.tabular_data(merged, columns=['FIPS_Code', 'Median_Household_Income_2011'])

vis.to_json(path)

 

?

明顯很多高收入?yún)^(qū)域在東海岸或是其他高密度區(qū)域。我敢打賭,在城市層級(jí)這將更加有趣,但這需要等以后發(fā)布的版本。讓我們快速重置地圖,再看看國(guó)家失業(yè)率:

#Swap county data for state data, reset map

state_data = pd.read_csv(state_unemployment)

vis.tabular_data(state_data, columns=['State', 'Unemployment'])

vis.geo_data(bind_data='data.id', reset=True, states=state_geo)

vis.update_map(scale=1000, projection='albersUsa')

vis + (['#c9cedb', '#0b0d11'], 'scales', 0, 'range')

vis.to_json(path)

?

地圖即是我的激情所在——我希望 Vincent 能夠更強(qiáng),包含輕松的添加點(diǎn)、標(biāo)記及其它的能力。如果各位讀者對(duì)于映射方面有什么功能上的需求,可以在Github上給我發(fā)問(wèn)題。 

責(zé)任編輯:龐桂玉 來(lái)源: 36大數(shù)據(jù)
相關(guān)推薦

2017-09-01 19:49:50

Python工具地圖

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2014-01-17 10:36:39

2020-02-21 16:51:58

前端可視化傳染病

2021-06-21 09:37:05

代碼開(kāi)源可視化

2022-03-01 10:29:44

Kubernetes容器

2021-03-18 08:11:18

PythonDash工具

2022-08-26 09:15:58

Python可視化plotly

2022-09-21 23:29:15

Python點(diǎn)云數(shù)據(jù)

2021-03-17 08:07:56

Python可視化工具

2015-10-29 09:36:48

2023-02-07 11:44:02

2014-06-20 15:00:57

數(shù)據(jù)可視化

2015-11-11 14:26:31

數(shù)據(jù)可視化術(shù)語(yǔ)

2020-06-15 14:10:29

Web 開(kāi)發(fā)可視化

2010-07-30 14:00:41

Flex組件

2010-08-12 13:52:38

Flex組件

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2021-03-25 07:30:24

代碼開(kāi)發(fā)數(shù)據(jù)

2009-04-21 14:26:41

可視化監(jiān)控IT管理摩卡
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)