当前位置: 首页 > 科技观察

10行Python代码创建可视化地图

时间:2023-03-15 23:47:14 科技观察

importvincentworld_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)当我开始构建Vincent时,我的目标之一是使地图构建尽可能合理。有一些很棒的python地图库-请参阅Basemap和Kartograph以使地图更有趣。我强烈推荐这两个工具,因为它们都很好用,而且非常强大。我想有更简单的工具可以依赖Vega的强大功能,并允许简单的语法点指向geoJSON文件,详细说明投影和大小/比率列,***输出地图。例如,对地图数据进行分层以创建更复杂的地图:vis=vincent.Map(width=1000,height=800)#AddtheUScountydataandanewlinecolorvis.geo_data(projection='albersUsa',scale=1000,counties=county_geo)vis+('2B4ECF','marks',0,'properties','enter','stroke','value')#Addthestatedata,removethefill,writeVegaspecoutputtoJSONvis.geo_data(states=state_geo)vis-('fill','marks',1,'properties','enter')vis.to_json(path)另外等高线图需要绑定Pandas数据,数据列需要直接映射到地图元素。假设有一个geoJSON到列数据的1:1映射,它的语法很简单:#'merged'isthePandasDataFramevis=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)我们的数据并非没有争议-用户需要确保geoJSON键和pandas数据帧之间存在1:1映射。这是上一个例子所需的简明数据框映射:我们的国家信息是一个CSV文件,其中列出了FIPS代码、国家名称和经济信息(列名省略):00000,US,UnitedStates,154505871,140674478,13831393,9,50502,10001000,AL,Alabama,2190519,1993977,196542,9,41427,10001001,AL,AutaugaCounty,25930,23854,2076,8,48863,117.901003,AL,鲍德温县,814,1215,4,6AL,BarbourCounty,9761,8651,1110,11.4,30117,72.7在geoJSON中,我们的国家/地区形状是FIPS代码的id(感谢Trifacta提供的信息)。为简单起见,简化了实际形状,完整的数据集可以在示例数据中找到:{"type":"FeatureCollection","features":[{"type":"Feature","id":"1001","properties":{"name":"Autauga"}{"type":"Feature","id":"1003","properties":{"name":"Baldwin"}{"type":“功能”,“ID”:“1005”,“属性”:{“名称”:“Barbour”}{“类型”:“功能”,“ID”:“1007”,“属性”:{“名称”":"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":"切诺基""}我们需要匹配FIPS代码,确保匹配正确,否则Vega无法正确压缩数据:importjsonimportpandasaspd#Mapthecountycodeswehaveinourgeometrytothoseinthe#county_datafile,whichcontainsadditionalrowsweedon'tneedwithopen(county_geo,'r')asf:get_id=json。load(f)#GrabtheFIPScodesandloadthemintoadatacodeframe=countx['id']forxinget_id['features']]county_df=pd.DataFrame({'FIPS_Code':county_codes},dtype=str)#ReadintoDataframe,casttostringforconsistencydf=pd.read_csv(county_data,na_values=[''])df['FIPS_Code']=df['FIPS_Code'].astype(str)#Performaninnerjoin,padNA'swithdatafromnearestcountymerged=pd.merge(df,county_df,on='FIPS_Code',how='inner')merged=merged.fillna(method='pad')>>>merged.head()FIPS_CodeStateArea_nameCivilian_labor_force_2011Employed_2011\01001ALAutaugaCounty259302385411003ALBaldwinCounty854077849121005ALBarbourCounty9761865131007ALBibbCounty9216830341009ALBlountCounty2634724156Unemployed_2011Unemployment_rate_2011Median_Household_Income_2011\020768.048863169168.1501442111011.43011739139.937347421918.341940Med_HH_Income_Percent_of_StateTotal_20110117.91121.0272.7390.24101.2现在,我们可以快速生成不同的等值线:vis.tabular_data(merged,columns=['FIPS_Code','Civilian_labor_force_2011'])vis.to_json(path)这只它可以告诉我们LA和King非常大而且人口稠密。让我们看看家庭收入中位数:vis.tabular_data(merged,columns=['FIPS_Code','Median_Household_Income_2011'])vis.to_json(path)显然有很多高收入地区在东海岸或其他高-密度区。我打赌它在城市层面会更有趣,但这将不得不等待以后的发布。让我们快速重置地图并查看州失业率:#Swapcountydataforstatedata,resetmapstate_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能做得更好,包括轻松添加点、标记等的能力。如果读者对映射有什么功能需求,可以在Github上给我发问题。

猜你喜欢