Python中怎么創(chuàng)建一個(gè)可視化地圖

Python中怎么創(chuàng)建一個(gè)可視化地圖,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

新邱ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

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

例如, 將地圖數(shù)據(jù)分層來建立更復(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)

Python中怎么創(chuàng)建一個(gè)可視化地圖

加之,等值線地圖需綁定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)

Python中怎么創(chuàng)建一個(gè)可視化地圖

我們的數(shù)據(jù)并非沒有爭(zhēng)議無需改造——用戶需要確保 geoJSON  鍵與熊貓數(shù)據(jù)框架之間具有1:1的映射。下面就是之前實(shí)例所需的簡(jiǎn)明的數(shù)據(jù)框架映射:我們的國(guó)家信息是一個(gè)列有FIPS 碼、國(guó)家名稱、以及經(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 無法正確的壓縮數(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)

Python中怎么創(chuàng)建一個(gè)可視化地圖

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

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

Python中怎么創(chuàng)建一個(gè)可視化地圖

明顯很多高收入?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)

Python中怎么創(chuàng)建一個(gè)可視化地圖

關(guān)于Python中怎么創(chuàng)建一個(gè)可視化地圖問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前標(biāo)題:Python中怎么創(chuàng)建一個(gè)可視化地圖
網(wǎng)頁(yè)鏈接:http://bm7419.com/article24/psccce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、網(wǎng)站維護(hù)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)