Chloropleth Maps

Sat 17 May 2025

title: "Chloropleth Maps" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


import geopandas
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world.head()
pop_est continent name iso_a3 gdp_md_est geometry
0 920938 Oceania Fiji FJI 8374.0 (POLYGON ((180 -16.06713266364245, 180 -16.555...
1 53950935 Africa Tanzania TZA 150600.0 POLYGON ((33.90371119710453 -0.950000000000000...
2 603253 Africa W. Sahara ESH 906.5 POLYGON ((-8.665589565454809 27.65642588959236...
3 35623680 North America Canada CAN 1674000.0 (POLYGON ((-122.84 49.00000000000011, -122.974...
4 326625791 North America United States of America USA 18560000.0 (POLYGON ((-122.84 49.00000000000011, -120 49....
world = world[(world.pop_est > 0) & (world.continent != 'Antarctica')]
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
world.plot(column='gdp_per_cap')
<matplotlib.axes._subplots.AxesSubplot at 0x126780ac8>

png

world.continent.unique()
array(['Oceania', 'Africa', 'North America', 'Asia', 'South America',
       'Europe', 'Seven seas (open ocean)'], dtype=object)
# Show only Asia
asia = world[world.continent == 'Asia']
asia['gdp_per_cap'] = asia.gdp_md_est / asia.pop_est
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.
asia.plot(column='gdp_per_cap')
<matplotlib.axes._subplots.AxesSubplot at 0x1268cf668>

png

# Show only Europe
eu = world[world.continent == 'Europe']
eu['gdp_per_cap'] = eu.gdp_md_est / eu.pop_est
eu.plot(column='gdp_per_cap')
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy






<matplotlib.axes._subplots.AxesSubplot at 0x126a12ef0>

png

# South America

sa = world[world.continent == 'South America']
sa['gdp_per_cap'] = sa.gdp_md_est / sa.pop_est
sa.plot(column='gdp_per_cap')
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.





<matplotlib.axes._subplots.AxesSubplot at 0x126bc0470>

png

# With Legend
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

sa = world[world.continent == 'South America']
sa['gdp_per_cap'] = sa.gdp_md_est / sa.pop_est

sa.plot(column = 'pop_est', ax=ax, legend=True)
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.





<matplotlib.axes._subplots.AxesSubplot at 0x126b8c588>

png

fig, ax = plt.subplots(1, 1)

asia = world[world.continent == 'Asia']
asia['gdp_per_cap'] = asia.gdp_md_est / asia.pop_est

asia.plot(column = 'pop_est', ax=ax, legend=True)
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.





<matplotlib.axes._subplots.AxesSubplot at 0x127002ef0>

png

fig, ax = plt.subplots(1, 1)

asia = world[world['continent'] == 'Asia']
asia['gdp_per_cap'] = asia['gdp_md_est'] / asia['pop_est']

from mpl_toolkits.axes_grid1 import make_axes_locatable

divider = make_axes_locatable(ax)

cax = divider.append_axes("right", size="4%", pad=0.1)

asia.plot(column = 'pop_est', ax=ax, legend=True, cax=cax)
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.





<matplotlib.axes._subplots.AxesSubplot at 0x127e412e8>

png

fig, ax = plt.subplots(1, 1)

asia = world[world['continent'] == 'Asia']
asia['gdp_per_cap'] = asia['gdp_md_est'] / asia['pop_est']

from mpl_toolkits.axes_grid1 import make_axes_locatable

divider = make_axes_locatable(ax)

cax = divider.append_axes("right", size="4%", pad=0.1)

asia.plot(column = 'pop_est', ax=ax, legend=True, cax=cax, cmap='YlOrRd')
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.





<matplotlib.axes._subplots.AxesSubplot at 0x1282e8f98>

png


Score: 20

Category: geopandas


City Location

Sat 17 May 2025

title: "City Location" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


from geopy.geocoders import Nominatim
geoloc = Nominatim(user_agent="rajarcsp", format_string="%s, Toronto ON")
address, (lat, long) = geoloc.geocode("26, Spadina Road")
address
'Spadina Road, The Annex, University—Rosedale, Old Toronto, Toronto, Ontario, M5R 2X3, Canada'


Score …

Category: geopandas

Read More

Geo Distance

Sat 17 May 2025

title: "Geo Distance" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="rajarcsp")
from geopy import distance
l_spadina = geolocator.geocode("28 Spadina Road")
l_spadina_latlong = l_spadina.latitude, l_spadina.longitude
l_spadina_latlong
(43.6966329769175, -79.7940236288173)
l_north_york = geolocator.geocode("5000 Yonge Street")
l_north_york_latlong …

Category: geopandas

Read More

Geo Distance Method

Sat 17 May 2025

title: "Geo Distance Method" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="rajarcsp")
from geopy import distance
def get_distance(one, two):
    cone = geolocator.geocode(one)
    ctwo = geolocator.geocode(two)

    one_latlong = cone.latitude, ctwo.longitude
    two_latlong = ctwo.latitude, ctwo.longitude …

Category: geopandas

Read More

Geopandas Canada

Sat 17 May 2025

title: "Geo Pandas Canada" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


import pandas as pd
import geopandas
import matplotlib.pyplot as plt
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="rajarcsp")
def get_lat_lang(city):
    location = geolocator.geocode(city)
    return location.latitude, location.longitude
to_lat, to_long = get_lat_lang …

Category: geopandas

Read More

Geopandas Simple

Sat 17 May 2025

title: "Geo Pandas Simple" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


import pandas as pd
import geopandas
import matplotlib.pyplot as plt
df = pd.DataFrame(
    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
     'Latitude': [-34.58, -15.78, -33.45, 4 …

Category: geopandas

Read More

Geopy Simple

Sat 17 May 2025

title: "Template" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="rajarcsp")
location = geolocator.geocode("175 5th Avenue NYC")
location
Location(Flatiron Building, 175, 5th Avenue, Flatiron District, Manhattan, Manhattan Community Board 5, New York County, NYC, New York, 10010 …

Category: geopandas

Read More

World Map

Sat 17 May 2025

title: "World Map" author: "Raja CSP Raman" date: 2019-05-07 description: "-" type: technical_note draft: false


import geopandas
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world
pop_est continent name iso_a3 gdp_md_est geometry
0 920938 …

Category: geopandas

Read More
Page 1 of 1