Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a visual map in Python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to create a visual map in Python? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Import vincent world_countries = ringing world words countries.json 'world= vincent.Map (width=1200, height=1000) world.geo_data (projection='winkel3', scale=200, world=world_countries) world.to_json (path)

When I started building Vincent, one of my goals was to rationalize the construction of maps as much as possible. There are some great python map libraries-see Basemap and Kartograph to make maps more interesting. I highly recommend these two tools because they are both easy to use and powerful. I would like to have a simpler tool that can rely on the power of Vega and allow simple grammar points to geoJSON files, describe a projection and size / column in detail, and * * output maps.

For example, layer map data to create more complex maps:

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)

In addition, isoline maps need to be bound to Pandas data and data columns need to be mapped directly to map elements. Suppose you have a 1:1 mapping from geoJSON to column data, and its syntax is very simple:

# '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)

Our data is not without controversy and without modification-users need to make sure that there is a 1:1 mapping between the geoJSON key and the Panda data framework. Here is the concise data framework mapping required by the previous example: our country information is an CSV file with FIPS code, country name, and economic information (list omitted):

00000,US,United States,154505871,140674478,13831393,9,50502100 01000,AL,Alabama,2190519,1993977,196542,9,41427100 01001,AL,Autauga County,25930,23854,2076,8,48863117.9 01003,AL,Baldwin County,85407,78491,6916,8.1,50144121 01005,AL,Barbour County,9761,8651,1110,11.4,30117,72.7

In geoJSON, the shape of our country is id with FIPS code (thanks to fork for the relevant information from Trifacta). For simplicity, the actual shape has been simplified, and the complete dataset can be found in the sample data:

{"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": "Butler"} {"type": "Feature", "id": "1015" "properties": {"name": "Calhoun"} {"type": "Feature", "id": "1017", "properties": {"name": "Chambers"} {"type": "Feature", "id": "1019", "properties": {"name": "Cherokee"}

We need to match the FIPS code to make sure the match is correct, otherwise Vega will not be able to compress the data correctly:

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\ 020768.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

Now, we can quickly generate different contours:

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

This can only tell us that LA and King are very large and densely populated. Let's take a look at the middle household income:

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

Obviously many high-income areas are on the East Coast or other high-density areas. I bet it will be more interesting at the city level, but it will need to be released later. Let's quickly reset the map and look at the national unemployment rate:

# 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 + (['# c9cedbails,'# 0b0d11'], 'scales', 0,' range') vis.to_json (path)

This is the answer to the question about how to create a visual map in Python. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report