5

I am hosting a conference and want to show a map of where the attendees have arrived from. I have an input file with the names of the cities. I'd like to put a dot on each city, and have the size of the dot be proportional to the number of attendees. So my input looks like this:

Chicago 10
Philadelphia 20
New York 5 
San Francisco 25

I'd like to do this with Python, which I believe means I should be using matplotlib and basemap. I've found sample code that has shapefiles for the US, but it's not clear to me how to get the lat/lon data US cities. Do I have to implement this all from scratch? It seems like a fairly common thing to do.

  • Any chance you can upload the shape file that you used? – ryan purcell Nov 29 '17 at 14:23
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – aldo_tapia Nov 29 '17 at 14:40
  • Huh? @aldo_tapia, what do you mean? – vy32 Nov 30 '17 at 1:33
  • @vy32 comment above was generated from a review task. Ryan posted his comment as an answer and the review task moved it to here. – aldo_tapia Nov 30 '17 at 10:18
  • Well it is confusing. However, @ryan purcell will be happy to know that the shape file is part of mpl_toolkits.basemap – vy32 Nov 30 '17 at 23:33
5

I was able to use the API to put together the program I wanted. Here it is:

# Draw the locations of cities on a map of the US

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from geopy.geocoders import Nominatim
import math

cities = [["Chicago",10],
          ["Boston",10],
          ["New York",5],
          ["San Francisco",25]]
scale = 5

map = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
        projection='lcc',lat_1=32,lat_2=45,lon_0=-95)

# load the shapefile, use the name 'states'
map.readshapefile('st99_d00', name='states', drawbounds=True)

# Get the location of each city and plot it
geolocator = Nominatim()
for (city,count) in cities:
    loc = geolocator.geocode(city)
    x, y = map(loc.longitude, loc.latitude)
    map.plot(x,y,marker='o',color='Red',markersize=int(math.sqrt(count))*scale)
plt.show()

And here is the result: enter image description here

|improve this answer|||||
  • 1
    Nice! Short, sweet, gets the job done nicely. – Chad Cooper Jun 15 '16 at 18:24
  • 1
    Thanks. I need to build up a library of these kinds of easy-to-do things. It's strange that they aren't in the standard examples. – vy32 Jun 15 '16 at 19:20
  • From where did you obtain the shapefile? – DataGirl Jul 30 '19 at 16:22
1

With Python, you could use the geopy module:

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> loc = geolocator.geocode("New York, NY")
>>> loc
Location((40.7305991, -73.9865811, 0.0))

You could also use something like a batch geocoder, which for inputs such as:

Chicago, IL
Philadelphia, PA
New York, NY
San Francisco, CA

gives you:

"Chicago, IL",41.878114,-87.629798
"Philadelphia, PA",39.952584,-75.165222
"New York, NY",40.712784,-74.005941
"San Francisco, CA",37.77493,-122.419416

Either way, you get lat/lons to feed into matplotlib.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.